]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0 image_mixer: Refactored, core: Fixed destruction proxy usage.
[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 #include <common/utility/move_on_copy.h>\r
34 \r
35 #include <boost/foreach.hpp>\r
36 \r
37 #include <tbb/parallel_for_each.h>\r
38 \r
39 #include <map>\r
40 #include <set>\r
41 \r
42 namespace caspar { namespace core {\r
43 \r
44 struct stage::implementation : boost::noncopyable\r
45 {               \r
46         std::map<int, layer>                                            layers_;        \r
47         video_channel_context&                                          channel_;\r
48 public:\r
49         implementation(video_channel_context& video_channel)  \r
50                 : channel_(video_channel)\r
51         {\r
52         }\r
53                                                 \r
54         std::map<int, safe_ptr<basic_frame>> execute()\r
55         {               \r
56                 try\r
57                 {\r
58                         std::map<int, safe_ptr<basic_frame>> frames;\r
59                 \r
60                         BOOST_FOREACH(auto& layer, layers_)                     \r
61                                 frames[layer.first] = basic_frame::empty();     \r
62 \r
63                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
64                         {\r
65                                 frames[layer.first] = layer.second.receive();   \r
66                         });\r
67 \r
68                         return frames;\r
69                 }\r
70                 catch(...)\r
71                 {\r
72                         CASPAR_LOG(error) << L"[stage] Error detected";\r
73                         throw;\r
74                 }               \r
75         }\r
76 \r
77         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
78         {\r
79                 channel_.execution().invoke([&]\r
80                 {\r
81                         layers_[index].load(create_destroy_producer_proxy(channel_.destruction(), producer), preview, auto_play_delta);\r
82                 }, high_priority);\r
83         }\r
84 \r
85         void pause(int index)\r
86         {               \r
87                 channel_.execution().invoke([&]\r
88                 {\r
89                         layers_[index].pause();\r
90                 }, high_priority);\r
91         }\r
92 \r
93         void play(int index)\r
94         {               \r
95                 channel_.execution().invoke([&]\r
96                 {\r
97                         layers_[index].play();\r
98                 }, high_priority);\r
99         }\r
100 \r
101         void stop(int index)\r
102         {               \r
103                 channel_.execution().invoke([&]\r
104                 {\r
105                         layers_[index].stop();\r
106                 }, high_priority);\r
107         }\r
108 \r
109         void clear(int index)\r
110         {\r
111                 channel_.execution().invoke([&]\r
112                 {\r
113                         layers_.erase(index);\r
114                 }, high_priority);\r
115         }\r
116                 \r
117         void clear()\r
118         {\r
119                 channel_.execution().invoke([&]\r
120                 {\r
121                         layers_.clear();\r
122                 }, high_priority);\r
123         }       \r
124         \r
125         void swap_layer(int index, size_t other_index)\r
126         {\r
127                 channel_.execution().invoke([&]\r
128                 {\r
129                         std::swap(layers_[index], layers_[other_index]);\r
130                 }, high_priority);\r
131         }\r
132 \r
133         void swap_layer(int index, size_t other_index, stage& other)\r
134         {\r
135                 if(other.impl_.get() == this)\r
136                         swap_layer(index, other_index);\r
137                 else\r
138                 {\r
139                         auto func = [&]\r
140                         {\r
141                                 std::swap(layers_[index], other.impl_->layers_[other_index]);\r
142                         };              \r
143                         channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
144                 }\r
145         }\r
146 \r
147         void swap(stage& other)\r
148         {\r
149                 if(other.impl_.get() == this)\r
150                         return;\r
151                 \r
152                 auto func = [&]\r
153                 {\r
154                         std::swap(layers_, other.impl_->layers_);\r
155                 };              \r
156                 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func, high_priority);}, high_priority);\r
157         }\r
158 \r
159         layer_status get_status(int index)\r
160         {               \r
161                 return channel_.execution().invoke([&]\r
162                 {\r
163                         return layers_[index].status();\r
164                 }, high_priority );\r
165         }\r
166         \r
167         safe_ptr<frame_producer> foreground(int index)\r
168         {\r
169                 return channel_.execution().invoke([=]{return layers_[index].foreground();}, high_priority);\r
170         }\r
171         \r
172         safe_ptr<frame_producer> background(int index)\r
173         {\r
174                 return channel_.execution().invoke([=]{return layers_[index].background();}, high_priority);\r
175         }\r
176 \r
177         std::wstring print() const\r
178         {\r
179                 return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
180         }\r
181 \r
182 };\r
183 \r
184 stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
185 void stage::swap(stage& other){impl_->swap(other);}\r
186 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
187 void stage::pause(int index){impl_->pause(index);}\r
188 void stage::play(int index){impl_->play(index);}\r
189 void stage::stop(int index){impl_->stop(index);}\r
190 void stage::clear(int index){impl_->clear(index);}\r
191 void stage::clear(){impl_->clear();}\r
192 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
193 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
194 layer_status stage::get_status(int index){return impl_->get_status(index);}\r
195 safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}\r
196 safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}\r
197 std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}\r
198 }}