]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
6c7816ba91d345de3c42f583f86efb1fcf3e0759
[casparcg] / core / producer / stage.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 "stage.h"\r
24 \r
25 #include "layer.h"\r
26 \r
27 #include "../video_channel_context.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/foreach.hpp>\r
35 \r
36 #include <tbb/parallel_for_each.h>\r
37 \r
38 #include <map>\r
39 #include <set>\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(debug) << 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(int hints)                                                                                              {return producer_->receive(hints);}\r
69         virtual safe_ptr<basic_frame>           last_frame() const                                                                                              {return producer_->last_frame();}\r
70         virtual std::wstring                            print() const                                                                                                   {return producer_->print();}\r
71         virtual void                                            param(const std::wstring& str)                                                                  {producer_->param(str);}\r
72         virtual safe_ptr<frame_producer>        get_following_producer() const                                                                  {return producer_->get_following_producer();}\r
73         virtual void                                            set_leading_producer(const safe_ptr<frame_producer>& producer)  {producer_->set_leading_producer(producer);}\r
74         virtual int64_t                                         nb_frames() const                                                                                               {return producer_->nb_frames();}\r
75 };\r
76 \r
77 struct stage::implementation : boost::noncopyable\r
78 {               \r
79         std::map<int, layer>                                            layers_;        \r
80         video_channel_context&                                          channel_;\r
81 public:\r
82         implementation(video_channel_context& video_channel)  \r
83                 : channel_(video_channel)\r
84         {\r
85         }\r
86                                                 \r
87         std::map<int, safe_ptr<basic_frame>> execute()\r
88         {               \r
89                 try\r
90                 {\r
91                         std::map<int, safe_ptr<basic_frame>> frames;\r
92                 \r
93                         BOOST_FOREACH(auto& layer, layers_)                     \r
94                                 frames[layer.first] = basic_frame::empty();     \r
95 \r
96                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
97                         {\r
98                                 frames[layer.first] = layer.second.receive();   \r
99                         });\r
100 \r
101                         return frames;\r
102                 }\r
103                 catch(...)\r
104                 {\r
105                         CASPAR_LOG(error) << L"[stage] Error detected";\r
106                         throw;\r
107                 }               \r
108         }\r
109 \r
110         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
111         {\r
112                 channel_.execution().invoke([&]\r
113                 {\r
114                         layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview, auto_play_delta);\r
115                 }, high_priority);\r
116         }\r
117 \r
118         void pause(int index)\r
119         {               \r
120                 channel_.execution().invoke([&]\r
121                 {\r
122                         layers_[index].pause();\r
123                 }, high_priority);\r
124         }\r
125 \r
126         void play(int index)\r
127         {               \r
128                 channel_.execution().invoke([&]\r
129                 {\r
130                         layers_[index].play();\r
131                 }, high_priority);\r
132         }\r
133 \r
134         void stop(int index)\r
135         {               \r
136                 channel_.execution().invoke([&]\r
137                 {\r
138                         layers_[index].stop();\r
139                 }, high_priority);\r
140         }\r
141 \r
142         void clear(int index)\r
143         {\r
144                 channel_.execution().invoke([&]\r
145                 {\r
146                         layers_.erase(index);\r
147                 }, high_priority);\r
148         }\r
149                 \r
150         void clear()\r
151         {\r
152                 channel_.execution().invoke([&]\r
153                 {\r
154                         layers_.clear();\r
155                 }, high_priority);\r
156         }       \r
157         \r
158         void swap_layer(int index, size_t other_index)\r
159         {\r
160                 channel_.execution().invoke([&]\r
161                 {\r
162                         std::swap(layers_[index], layers_[other_index]);\r
163                 }, high_priority);\r
164         }\r
165 \r
166         void swap_layer(int index, size_t other_index, stage& other)\r
167         {\r
168                 if(other.impl_.get() == this)\r
169                         swap_layer(index, other_index);\r
170                 else\r
171                 {\r
172                         auto func = [&]\r
173                         {\r
174                                 std::swap(layers_[index], other.impl_->layers_[other_index]);\r
175                         };              \r
176                         channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
177                 }\r
178         }\r
179 \r
180         void swap(stage& other)\r
181         {\r
182                 if(other.impl_.get() == this)\r
183                         return;\r
184                 \r
185                 auto func = [&]\r
186                 {\r
187                         std::swap(layers_, other.impl_->layers_);\r
188                 };              \r
189                 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
190         }\r
191 \r
192         layer_status get_status(int index)\r
193         {               \r
194                 return channel_.execution().invoke([&]\r
195                 {\r
196                         return layers_[index].status();\r
197                 }, high_priority );\r
198         }\r
199         \r
200         safe_ptr<frame_producer> foreground(int index)\r
201         {\r
202                 return channel_.execution().invoke([=]{return layers_[index].foreground();}, high_priority);\r
203         }\r
204         \r
205         safe_ptr<frame_producer> background(int index)\r
206         {\r
207                 return channel_.execution().invoke([=]{return layers_[index].background();}, high_priority);\r
208         }\r
209 \r
210         std::wstring print() const\r
211         {\r
212                 return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
213         }\r
214 \r
215 };\r
216 \r
217 stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
218 void stage::swap(stage& other){impl_->swap(other);}\r
219 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
220 void stage::pause(int index){impl_->pause(index);}\r
221 void stage::play(int index){impl_->play(index);}\r
222 void stage::stop(int index){impl_->stop(index);}\r
223 void stage::clear(int index){impl_->clear(index);}\r
224 void stage::clear(){impl_->clear();}\r
225 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
226 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
227 layer_status stage::get_status(int index){return impl_->get_status(index);}\r
228 safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}\r
229 safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}\r
230 std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}\r
231 }}