]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer_device.cpp
d35abdbdc87ec767656a88ece2700d29a175942c
[casparcg] / core / producer / frame_producer_device.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "frame_producer_device.h"\r
24 \r
25 #include "layer.h"\r
26 #include "destroy_producer_proxy.h"\r
27 \r
28 #include <core/producer/frame/basic_frame.h>\r
29 #include <core/producer/frame/frame_factory.h>\r
30 \r
31 #include <common/diagnostics/graph.h>\r
32 #include <common/concurrency/executor.h>\r
33 \r
34 #include <boost/timer.hpp>\r
35 \r
36 #include <tbb/parallel_for.h>\r
37 \r
38 #include <map>\r
39 \r
40 namespace caspar { namespace core {\r
41 \r
42 struct frame_producer_device::implementation : boost::noncopyable\r
43 {               \r
44         std::map<int, layer>             layers_;               \r
45         const video_format_desc          format_desc_;  \r
46         const output_t                           output_;\r
47         \r
48         safe_ptr<diagnostics::graph> diag_;\r
49         boost::timer                             frame_timer_;\r
50         boost::timer                             tick_timer_;\r
51         boost::timer                             output_timer_;\r
52         \r
53         executor                                        executor_;\r
54 public:\r
55         implementation(const video_format_desc& format_desc, const output_t& output)  \r
56                 : format_desc_(format_desc)\r
57                 , diag_(diagnostics::create_graph(std::string("frame_producer_device")))\r
58                 , executor_(L"frame_producer_device")\r
59                 , output_(output)\r
60         {\r
61                 diag_->add_guide("frame-time", 0.5f);   \r
62                 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
63                 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
64                 diag_->set_color("output-time", diagnostics::color(0.5f, 1.0f, 0.2f));\r
65 \r
66                 executor_.set_priority_class(above_normal_priority_class);\r
67                 executor_.begin_invoke([=]{tick();});           \r
68         }\r
69                         \r
70         void tick()\r
71         {                               \r
72                 try\r
73                 {\r
74                         auto frame = render();\r
75                         output_timer_.restart();\r
76                         output_(frame);\r
77                         diag_->update_value("output-time", static_cast<float>(output_timer_.elapsed()*format_desc_.fps*0.5));\r
78                 }\r
79                 catch(...)\r
80                 {\r
81                         CASPAR_LOG_CURRENT_EXCEPTION();\r
82                 }\r
83 \r
84                 executor_.begin_invoke([=]{tick();});\r
85         }\r
86                         \r
87         std::map<int, safe_ptr<basic_frame>> render()\r
88         {       \r
89                 frame_timer_.restart();\r
90 \r
91                 std::map<int, safe_ptr<basic_frame>> frames;\r
92                 BOOST_FOREACH(auto& layer, layers_)\r
93                         frames[layer.first] = basic_frame::empty();\r
94 \r
95                 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](decltype(*layers_.begin())& pair)\r
96                 {\r
97                         frames[pair.first] = pair.second.receive();\r
98                 });\r
99                 \r
100                 diag_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
101 \r
102                 diag_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
103                 tick_timer_.restart();\r
104 \r
105                 return frames;\r
106         }\r
107 \r
108         void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
109         {\r
110                 executor_.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(producer), preview);});\r
111         }\r
112 \r
113         void pause(int index)\r
114         {               \r
115                 executor_.invoke([&]{layers_[index].pause();});\r
116         }\r
117 \r
118         void play(int index)\r
119         {               \r
120                 executor_.invoke([&]{layers_[index].play();});\r
121         }\r
122 \r
123         void stop(int index)\r
124         {               \r
125                 executor_.invoke([&]{layers_[index].stop();});\r
126         }\r
127 \r
128         void clear(int index)\r
129         {\r
130                 executor_.invoke([&]{layers_.erase(index);});\r
131         }\r
132                 \r
133         void clear()\r
134         {\r
135                 executor_.invoke([&]{layers_.clear();});\r
136         }       \r
137         \r
138         void swap_layer(int index, size_t other_index)\r
139         {\r
140                 executor_.invoke([&]{layers_[index].swap(layers_[other_index]);});\r
141         }\r
142 \r
143         void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
144         {\r
145                 if(other.impl_.get() == this)\r
146                         swap_layer(index, other_index);\r
147                 else\r
148                 {\r
149                         if(format_desc_ != other.impl_->format_desc_)\r
150                                 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
151 \r
152                         auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
153                 \r
154                         executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
155                 }\r
156         }\r
157 \r
158         void swap(frame_producer_device& other)\r
159         {\r
160                 if(other.impl_.get() == this)\r
161                         return;\r
162 \r
163                 if(format_desc_ != other.impl_->format_desc_)\r
164                         BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
165 \r
166                 auto func = [&]\r
167                 {\r
168                         auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
169 \r
170                         std::set<int> indices;\r
171                         auto inserter = std::inserter(indices, indices.begin());\r
172 \r
173                         std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
174                         std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
175                         std::for_each(indices.begin(), indices.end(), [&](int index)\r
176                         {\r
177                                 layers_[index].swap(other.impl_->layers_[index]);\r
178                         });                                     \r
179                 };\r
180                 \r
181                 executor_.invoke([&]{other.impl_->executor_.invoke(func);});\r
182         }\r
183         \r
184         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
185         {\r
186                 return executor_.begin_invoke([=]{return layers_[index].foreground();});\r
187         }\r
188         \r
189         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
190         {\r
191                 return executor_.begin_invoke([=]{return layers_[index].background();});\r
192         }\r
193 };\r
194 \r
195 frame_producer_device::frame_producer_device(const video_format_desc& format_desc, const output_t& output) : impl_(new implementation(format_desc, output)){}\r
196 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}\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 preview){impl_->load(index, producer, 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) {return impl_->foreground(index);}\r
207 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}\r
208 }}