]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0. stage: Reduced command execution latency.
[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 <map>\r
37 #include <set>\r
38 \r
39 namespace caspar { namespace core {\r
40                 \r
41 void destroy_producer(safe_ptr<frame_producer>& producer)\r
42 {\r
43         if(!producer.unique())\r
44                 CASPAR_LOG(debug) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
45         \r
46         producer = frame_producer::empty();\r
47 }\r
48 \r
49 class destroy_producer_proxy : public frame_producer\r
50 {\r
51         safe_ptr<frame_producer> producer_;\r
52         executor& destroy_context_;\r
53 public:\r
54         destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
55                 : producer_(producer)\r
56                 , destroy_context_(destroy_context){}\r
57 \r
58         ~destroy_producer_proxy()\r
59         {               \r
60                 if(destroy_context_.size() > 4)\r
61                         CASPAR_LOG(error) << L" Potential destroyer deadlock.";\r
62 \r
63                 destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
64         }\r
65 \r
66         virtual safe_ptr<basic_frame>           receive(int hints)                                                                                              {return producer_->receive(hints);}\r
67         virtual safe_ptr<basic_frame>           last_frame() const                                                                                              {return producer_->last_frame();}\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         virtual int64_t                                         nb_frames() const                                                                                               {return producer_->nb_frames();}\r
73 };\r
74 \r
75 struct stage::implementation : boost::noncopyable\r
76 {               \r
77         std::map<int, layer>                                            layers_;        \r
78         video_channel_context&                                          channel_;\r
79 public:\r
80         implementation(video_channel_context& video_channel)  \r
81                 : channel_(video_channel)\r
82         {\r
83         }\r
84                                                 \r
85         std::map<int, safe_ptr<basic_frame>> execute()\r
86         {                       \r
87                 std::map<int, safe_ptr<basic_frame>> frames;\r
88 \r
89                 try\r
90                 {\r
91                         BOOST_FOREACH(auto& layer, layers_)\r
92                         {\r
93                                 frames[layer.first] = layer.second.receive();\r
94                                 channel_.execution().yield();\r
95                         }\r
96                 }\r
97                 catch(...)\r
98                 {\r
99                         CASPAR_LOG_CURRENT_EXCEPTION();\r
100                 }\r
101                 \r
102                 return frames;\r
103         }\r
104 \r
105         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
106         {\r
107                 channel_.execution().invoke([&]\r
108                 {\r
109                         layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview, auto_play_delta);\r
110                 }, high_priority);\r
111         }\r
112 \r
113         void pause(int index)\r
114         {               \r
115                 channel_.execution().invoke([&]\r
116                 {\r
117                         layers_[index].pause();\r
118                 }, high_priority);\r
119         }\r
120 \r
121         void play(int index)\r
122         {               \r
123                 channel_.execution().invoke([&]\r
124                 {\r
125                         layers_[index].play();\r
126                 }, high_priority);\r
127         }\r
128 \r
129         void stop(int index)\r
130         {               \r
131                 channel_.execution().invoke([&]\r
132                 {\r
133                         layers_[index].stop();\r
134                 }, high_priority);\r
135         }\r
136 \r
137         void clear(int index)\r
138         {\r
139                 channel_.execution().invoke([&]\r
140                 {\r
141                         layers_.erase(index);\r
142                 }, high_priority);\r
143         }\r
144                 \r
145         void clear()\r
146         {\r
147                 channel_.execution().invoke([&]\r
148                 {\r
149                         layers_.clear();\r
150                 }, high_priority);\r
151         }       \r
152         \r
153         void swap_layer(int index, size_t other_index)\r
154         {\r
155                 channel_.execution().invoke([&]\r
156                 {\r
157                         layers_[index].swap(layers_[other_index]);\r
158                 }, high_priority);\r
159         }\r
160 \r
161         void swap_layer(int index, size_t other_index, stage& other)\r
162         {\r
163                 if(other.impl_.get() == this)\r
164                         swap_layer(index, other_index);\r
165                 else\r
166                 {\r
167                         if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
168                                 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
169 \r
170                         auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
171                 \r
172                         channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
173                 }\r
174         }\r
175 \r
176         void swap(stage& other)\r
177         {\r
178                 if(other.impl_.get() == this)\r
179                         return;\r
180                 \r
181                 if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc() || &channel_.ogl() != &other.impl_->channel_.ogl())\r
182                         BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between incompatible channels."));\r
183 \r
184                 auto func = [&]\r
185                 {\r
186                         auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
187 \r
188                         std::set<int> indices;\r
189                         auto inserter = std::inserter(indices, indices.begin());\r
190 \r
191                         std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
192                         std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
193 \r
194                         BOOST_FOREACH(auto index, indices)\r
195                                 layers_[index].swap(other.impl_->layers_[index]);\r
196                 };\r
197                 \r
198                 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);});\r
199         }\r
200         \r
201         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
202         {\r
203                 return channel_.execution().begin_invoke([=]{return layers_[index].foreground();}, high_priority);\r
204         }\r
205         \r
206         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
207         {\r
208                 return channel_.execution().begin_invoke([=]{return layers_[index].background();}, high_priority);\r
209         }\r
210 \r
211         std::wstring print() const\r
212         {\r
213                 return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
214         }\r
215 \r
216 };\r
217 \r
218 stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
219 void stage::swap(stage& other){impl_->swap(other);}\r
220 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
221 void stage::pause(int index){impl_->pause(index);}\r
222 void stage::play(int index){impl_->play(index);}\r
223 void stage::stop(int index){impl_->stop(index);}\r
224 void stage::clear(int index){impl_->clear(index);}\r
225 void stage::clear(){impl_->clear();}\r
226 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
227 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
228 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
229 boost::unique_future<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 }}