]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer_device.cpp
4d351ce19d6a2c400576fed5bffe57b4e1c5bc9c
[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 "../video_channel_context.h"\r
26 \r
27 #include "layer.h"\r
28 \r
29 #include <core/producer/frame/basic_frame.h>\r
30 #include <core/producer/frame/frame_factory.h>\r
31 \r
32 #include <common/diagnostics/graph.h>\r
33 #include <common/concurrency/executor.h>\r
34 \r
35 #include <boost/timer.hpp>\r
36 \r
37 #include <tbb/parallel_for.h>\r
38 \r
39 #include <map>\r
40 \r
41 namespace caspar { namespace core {\r
42                 \r
43 void destroy_producer(safe_ptr<frame_producer>& producer)\r
44 {\r
45         if(!producer.unique())\r
46                 CASPAR_LOG(warning) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
47                 \r
48         producer = frame_producer::empty();\r
49 }\r
50 \r
51 class destroy_producer_proxy : public frame_producer\r
52 {\r
53         safe_ptr<frame_producer> producer_;\r
54         executor& destroy_context_;\r
55 public:\r
56         destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
57                 : producer_(producer)\r
58                 , destroy_context_(destroy_context){}\r
59 \r
60         ~destroy_producer_proxy()\r
61         {               \r
62                 if(destroy_context_.size() > 4)\r
63                         CASPAR_LOG(error) << L" Potential destroyer deadlock.";\r
64 \r
65                 destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
66         }\r
67 \r
68         virtual safe_ptr<basic_frame>           receive()                                                                                                               {return core::receive(producer_);}\r
69         virtual std::wstring                            print() const                                                                                                   {return producer_->print();}\r
70         virtual void                                            param(const std::wstring& str)                                                                  {producer_->param(str);}\r
71         virtual safe_ptr<frame_producer>        get_following_producer() const                                                                  {return producer_->get_following_producer();}\r
72         virtual void                                            set_leading_producer(const safe_ptr<frame_producer>& producer)  {producer_->set_leading_producer(producer);}\r
73 };\r
74 \r
75 struct frame_producer_device::implementation : boost::noncopyable\r
76 {               \r
77         std::map<int, layer>                                            layers_;                \r
78         typedef std::map<int, layer>::value_type        layer_t;\r
79         \r
80         safe_ptr<diagnostics::graph>                            diag_;\r
81         boost::timer                                                            frame_timer_;\r
82         boost::timer                                                            tick_timer_;\r
83         boost::timer                                                            output_timer_;\r
84         \r
85         video_channel_context&                                          channel_;\r
86 public:\r
87         implementation(video_channel_context& video_channel)  \r
88                 : diag_(diagnostics::create_graph(std::string("frame_producer_device")))\r
89                 , channel_(video_channel)\r
90         {\r
91                 diag_->add_guide("frame-time", 0.5f);   \r
92                 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
93                 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));    \r
94         }\r
95                                                 \r
96         std::map<int, safe_ptr<basic_frame>> operator()()\r
97         {       \r
98                 frame_timer_.restart();\r
99                 \r
100                 std::map<int, safe_ptr<basic_frame>> frames;\r
101 \r
102                 // Allocate placeholders.\r
103                 std::for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\r
104                 {\r
105                         frames[layer.first] = basic_frame::empty();\r
106                 });\r
107 \r
108                 // Render layers\r
109                 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\r
110                 {\r
111                         frames[layer.first] = layer.second.receive();\r
112                 });\r
113                 \r
114                 diag_->update_value("frame-time", frame_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
115 \r
116                 diag_->update_value("tick-time", tick_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
117                 tick_timer_.restart();\r
118 \r
119                 return frames;\r
120         }\r
121 \r
122         void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
123         {\r
124                 channel_.execution.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction, producer), preview);});\r
125         }\r
126 \r
127         void pause(int index)\r
128         {               \r
129                 channel_.execution.invoke([&]{layers_[index].pause();});\r
130         }\r
131 \r
132         void play(int index)\r
133         {               \r
134                 channel_.execution.invoke([&]{layers_[index].play();});\r
135         }\r
136 \r
137         void stop(int index)\r
138         {               \r
139                 channel_.execution.invoke([&]{layers_[index].stop();});\r
140         }\r
141 \r
142         void clear(int index)\r
143         {\r
144                 channel_.execution.invoke([&]{layers_.erase(index);});\r
145         }\r
146                 \r
147         void clear()\r
148         {\r
149                 channel_.execution.invoke([&]{layers_.clear();});\r
150         }       \r
151         \r
152         void swap_layer(int index, size_t other_index)\r
153         {\r
154                 channel_.execution.invoke([&]{layers_[index].swap(layers_[other_index]);});\r
155         }\r
156 \r
157         void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
158         {\r
159                 if(other.impl_.get() == this)\r
160                         swap_layer(index, other_index);\r
161                 else\r
162                 {\r
163                         if(channel_.format_desc != other.impl_->channel_.format_desc)\r
164                                 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
165 \r
166                         auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
167                 \r
168                         channel_.execution.invoke([&]{other.impl_->channel_.execution.invoke(func);});\r
169                 }\r
170         }\r
171 \r
172         void swap(frame_producer_device& other)\r
173         {\r
174                 if(other.impl_.get() == this)\r
175                         return;\r
176 \r
177                 if(channel_.format_desc != other.impl_->channel_.format_desc)\r
178                         BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
179 \r
180                 auto func = [&]\r
181                 {\r
182                         auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
183 \r
184                         std::set<int> indices;\r
185                         auto inserter = std::inserter(indices, indices.begin());\r
186 \r
187                         std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
188                         std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
189 \r
190                         std::for_each(indices.begin(), indices.end(), [&](int index)\r
191                         {\r
192                                 layers_[index].swap(other.impl_->layers_[index]);\r
193                         });                                     \r
194                 };\r
195                 \r
196                 channel_.execution.invoke([&]{other.impl_->channel_.execution.invoke(func);});\r
197         }\r
198         \r
199         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
200         {\r
201                 return channel_.execution.begin_invoke([=]{return layers_[index].foreground();});\r
202         }\r
203         \r
204         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
205         {\r
206                 return channel_.execution.begin_invoke([=]{return layers_[index].background();});\r
207         }\r
208 };\r
209 \r
210 frame_producer_device::frame_producer_device(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
211 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
212 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}\r
213 void frame_producer_device::pause(int index){impl_->pause(index);}\r
214 void frame_producer_device::play(int index){impl_->play(index);}\r
215 void frame_producer_device::stop(int index){impl_->stop(index);}\r
216 void frame_producer_device::clear(int index){impl_->clear(index);}\r
217 void frame_producer_device::clear(){impl_->clear();}\r
218 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
219 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
220 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) {return impl_->foreground(index);}\r
221 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}\r
222 std::map<int, safe_ptr<basic_frame>> frame_producer_device::operator()(){return (*impl_)();}\r
223 }}