]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer_device.cpp
8833456974ee5b62118c523f309d78f649f71f40
[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/concurrency/executor.h>\r
33 \r
34 #include <boost/range/algorithm.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 void destroy_producer(safe_ptr<frame_producer>& producer)\r
43 {\r
44         if(!producer.unique())\r
45                 CASPAR_LOG(warning) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
46                 \r
47         producer = frame_producer::empty();\r
48 }\r
49 \r
50 class destroy_producer_proxy : public frame_producer\r
51 {\r
52         safe_ptr<frame_producer> producer_;\r
53         executor& destroy_context_;\r
54 public:\r
55         destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
56                 : producer_(producer)\r
57                 , destroy_context_(destroy_context){}\r
58 \r
59         ~destroy_producer_proxy()\r
60         {               \r
61                 if(destroy_context_.size() > 4)\r
62                         CASPAR_LOG(error) << L" Potential destroyer deadlock.";\r
63 \r
64                 destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
65         }\r
66 \r
67         virtual safe_ptr<basic_frame>           receive()                                                                                                               {return core::receive(producer_);}\r
68         virtual std::wstring                            print() const                                                                                                   {return producer_->print();}\r
69         virtual void                                            param(const std::wstring& str)                                                                  {producer_->param(str);}\r
70         virtual safe_ptr<frame_producer>        get_following_producer() const                                                                  {return producer_->get_following_producer();}\r
71         virtual void                                            set_leading_producer(const safe_ptr<frame_producer>& producer)  {producer_->set_leading_producer(producer);}\r
72 };\r
73 \r
74 struct frame_producer_device::implementation : boost::noncopyable\r
75 {               \r
76         std::map<int, layer>                                            layers_;                \r
77         typedef std::map<int, layer>::value_type        layer_t;\r
78                 \r
79         video_channel_context&                                          channel_;\r
80 public:\r
81         implementation(video_channel_context& video_channel)  \r
82                 : channel_(video_channel)\r
83         {\r
84         }\r
85                                                 \r
86         std::map<int, safe_ptr<basic_frame>> execute()\r
87         {                       \r
88                 std::map<int, safe_ptr<basic_frame>> frames;\r
89 \r
90                 // Allocate placeholders.\r
91                 BOOST_FOREACH(auto layer, layers_)\r
92                         frames[layer.first] = basic_frame::empty();\r
93 \r
94                 // Render layers\r
95                 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\r
96                 {\r
97                         frames[layer.first] = layer.second.receive();\r
98                 });\r
99                 \r
100                 return frames;\r
101         }\r
102 \r
103         void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
104         {\r
105                 channel_.execution().invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview);});\r
106         }\r
107 \r
108         void pause(int index)\r
109         {               \r
110                 channel_.execution().invoke([&]{layers_[index].pause();});\r
111         }\r
112 \r
113         void play(int index)\r
114         {               \r
115                 channel_.execution().invoke([&]{layers_[index].play();});\r
116         }\r
117 \r
118         void stop(int index)\r
119         {               \r
120                 channel_.execution().invoke([&]{layers_[index].stop();});\r
121         }\r
122 \r
123         void clear(int index)\r
124         {\r
125                 channel_.execution().invoke([&]{layers_.erase(index);});\r
126         }\r
127                 \r
128         void clear()\r
129         {\r
130                 channel_.execution().invoke([&]{layers_.clear();});\r
131         }       \r
132         \r
133         void swap_layer(int index, size_t other_index)\r
134         {\r
135                 channel_.execution().invoke([&]{layers_[index].swap(layers_[other_index]);});\r
136         }\r
137 \r
138         void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
139         {\r
140                 if(other.impl_.get() == this)\r
141                         swap_layer(index, other_index);\r
142                 else\r
143                 {\r
144                         if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())\r
145                                 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
146 \r
147                         auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
148                 \r
149                         channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});\r
150                 }\r
151         }\r
152 \r
153         void swap(frame_producer_device& other)\r
154         {\r
155                 if(other.impl_.get() == this)\r
156                         return;\r
157 \r
158                 if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())\r
159                         BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
160 \r
161                 auto func = [&]\r
162                 {\r
163                         auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
164 \r
165                         std::set<int> indices;\r
166                         auto inserter = std::inserter(indices, indices.begin());\r
167 \r
168                         std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
169                         std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
170 \r
171                         BOOST_FOREACH(auto index, indices)\r
172                                 layers_[index].swap(other.impl_->layers_[index]);\r
173                 };\r
174                 \r
175                 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});\r
176         }\r
177         \r
178         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
179         {\r
180                 return channel_.execution().begin_invoke([=]{return layers_[index].foreground();});\r
181         }\r
182         \r
183         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
184         {\r
185                 return channel_.execution().begin_invoke([=]{return layers_[index].background();});\r
186         }\r
187 };\r
188 \r
189 frame_producer_device::frame_producer_device(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
190 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
191 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}\r
192 void frame_producer_device::pause(int index){impl_->pause(index);}\r
193 void frame_producer_device::play(int index){impl_->play(index);}\r
194 void frame_producer_device::stop(int index){impl_->stop(index);}\r
195 void frame_producer_device::clear(int index){impl_->clear(index);}\r
196 void frame_producer_device::clear(){impl_->clear();}\r
197 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
198 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
199 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) {return impl_->foreground(index);}\r
200 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}\r
201 std::map<int, safe_ptr<basic_frame>> frame_producer_device::execute(){return impl_->execute();}\r
202 }}