]> git.sesse.net Git - casparcg/blob - shell/server.cpp
Changed order between flash-producer and image-producer so that .ct files gets priority
[casparcg] / shell / server.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 \r
23 #include "server.h"\r
24 \r
25 #include <memory>\r
26 \r
27 #include <common/env.h>\r
28 #include <common/exception/exceptions.h>\r
29 #include <common/utility/string.h>\r
30 #include <common/filesystem/polling_filesystem_monitor.h>\r
31 \r
32 #include <core/mixer/gpu/ogl_device.h>\r
33 #include <core/mixer/audio/audio_util.h>\r
34 #include <core/mixer/mixer.h>\r
35 #include <core/video_channel.h>\r
36 #include <core/producer/stage.h>\r
37 #include <core/consumer/output.h>\r
38 #include <core/consumer/synchronizing/synchronizing_consumer.h>\r
39 #include <core/thumbnail_generator.h>\r
40 \r
41 #include <modules/bluefish/bluefish.h>\r
42 #include <modules/decklink/decklink.h>\r
43 #include <modules/ffmpeg/ffmpeg.h>\r
44 #include <modules/flash/flash.h>\r
45 #include <modules/portaudio/portaudio.h>\r
46 #include <modules/ogl/ogl.h>\r
47 #include <modules/silverlight/silverlight.h>\r
48 #include <modules/image/image.h>\r
49 #include <modules/image/consumer/image_consumer.h>\r
50 \r
51 #include <modules/portaudio/consumer/portaudio_consumer.h>\r
52 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
53 #include <modules/decklink/consumer/decklink_consumer.h>\r
54 #include <modules/decklink/consumer/blocking_decklink_consumer.h>\r
55 #include <modules/ogl/consumer/ogl_consumer.h>\r
56 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
57 \r
58 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
59 #include <protocol/cii/CIIProtocolStrategy.h>\r
60 #include <protocol/CLK/CLKProtocolStrategy.h>\r
61 #include <protocol/util/AsyncEventServer.h>\r
62 #include <protocol/util/stateful_protocol_strategy_wrapper.h>\r
63 #include <protocol/osc/client.h>\r
64 #include <protocol/asio/io_service_manager.h>\r
65 \r
66 #include <boost/algorithm/string.hpp>\r
67 #include <boost/lexical_cast.hpp>\r
68 #include <boost/foreach.hpp>\r
69 #include <boost/property_tree/ptree.hpp>\r
70 #include <boost/property_tree/xml_parser.hpp>\r
71 \r
72 namespace caspar {\r
73 \r
74 using namespace core;\r
75 using namespace protocol;\r
76 \r
77 struct server::implementation : boost::noncopyable\r
78 {\r
79         protocol::asio::io_service_manager                      io_service_manager_;\r
80         core::monitor::subject                                          monitor_subject_;\r
81         boost::promise<bool>&                                           shutdown_server_now_;\r
82         safe_ptr<ogl_device>                                            ogl_;\r
83         std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;     \r
84         std::shared_ptr<IO::AsyncEventServer>           primary_amcp_server_;\r
85         osc::client                                                                     osc_client_;\r
86         std::vector<std::shared_ptr<void>>                      predefined_osc_subscriptions_;\r
87         std::vector<safe_ptr<video_channel>>            channels_;\r
88         std::shared_ptr<thumbnail_generator>            thumbnail_generator_;\r
89 \r
90         implementation(boost::promise<bool>& shutdown_server_now)\r
91                 : shutdown_server_now_(shutdown_server_now)\r
92                 , ogl_(ogl_device::create())\r
93                 , osc_client_(io_service_manager_.service(), monitor_subject_)\r
94         {\r
95                 setup_audio(env::properties());\r
96 \r
97                 ffmpeg::init();\r
98                 CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
99                                                           \r
100                 bluefish::init();         \r
101                 CASPAR_LOG(info) << L"Initialized bluefish module.";\r
102                                                           \r
103                 decklink::init();         \r
104                 CASPAR_LOG(info) << L"Initialized decklink module.";\r
105 \r
106                 portaudio::init();\r
107                 CASPAR_LOG(info) << L"Initialized portaudio module.";\r
108                                                           \r
109                 ogl::init();              \r
110                 CASPAR_LOG(info) << L"Initialized ogl module.";\r
111 \r
112                 flash::init();            \r
113                 CASPAR_LOG(info) << L"Initialized flash module.";\r
114 \r
115                 image::init();            \r
116                 CASPAR_LOG(info) << L"Initialized image module.";\r
117 \r
118                 setup_channels(env::properties());\r
119                 CASPAR_LOG(info) << L"Initialized channels.";\r
120 \r
121                 setup_thumbnail_generation(env::properties());\r
122 \r
123                 setup_controllers(env::properties());\r
124                 CASPAR_LOG(info) << L"Initialized controllers.";\r
125 \r
126                 setup_osc(env::properties());\r
127                 CASPAR_LOG(info) << L"Initialized osc.";\r
128         }\r
129 \r
130         ~implementation()\r
131         {               \r
132                 thumbnail_generator_.reset();\r
133                 primary_amcp_server_.reset();\r
134                 async_servers_.clear();\r
135                 destroy_producers_synchronously();\r
136                 channels_.clear();\r
137 \r
138                 ffmpeg::uninit();\r
139         }\r
140 \r
141         void setup_audio(const boost::property_tree::wptree& pt)\r
142         {\r
143                 register_default_channel_layouts(default_channel_layout_repository());\r
144                 register_default_mix_configs(default_mix_config_repository());\r
145 \r
146                 auto channel_layouts =\r
147                         pt.get_child_optional(L"configuration.audio.channel-layouts");\r
148                 auto mix_configs =\r
149                         pt.get_child_optional(L"configuration.audio.mix-configs");\r
150 \r
151                 if (channel_layouts)\r
152                         parse_channel_layouts(\r
153                                         default_channel_layout_repository(), *channel_layouts);\r
154 \r
155                 if (mix_configs)\r
156                         parse_mix_configs(\r
157                                         default_mix_config_repository(), *mix_configs);\r
158         }\r
159                                 \r
160         void setup_channels(const boost::property_tree::wptree& pt)\r
161         {   \r
162                 using boost::property_tree::wptree;\r
163                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
164                 {               \r
165                         auto format_desc = video_format_desc::get(widen(xml_channel.second.get(L"video-mode", L"PAL")));                \r
166                         if(format_desc.format == video_format::invalid)\r
167                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
168                         auto audio_channel_layout = default_channel_layout_repository().get_by_name(\r
169                                         boost::to_upper_copy(xml_channel.second.get(L"channel-layout", L"STEREO")));\r
170                         \r
171                         channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_, audio_channel_layout));\r
172                         \r
173                         channels_.back()->monitor_output().link_target(&monitor_subject_);\r
174                         channels_.back()->mixer()->set_straight_alpha_output(\r
175                                         xml_channel.second.get(L"straight-alpha-output", false));\r
176 \r
177                         create_consumers(\r
178                                 xml_channel.second.get_child(L"consumers"),\r
179                                 [&] (const safe_ptr<core::frame_consumer>& consumer)\r
180                                 {\r
181                                         channels_.back()->output()->add(consumer);\r
182                                 });\r
183                 }\r
184 \r
185                 // Dummy diagnostics channel\r
186                 if(env::properties().get(L"configuration.channel-grid", false))\r
187                         channels_.push_back(make_safe<video_channel>(channels_.size()+1, core::video_format_desc::get(core::video_format::x576p2500), ogl_, default_channel_layout_repository().get_by_name(L"STEREO")));\r
188         }\r
189 \r
190         template<typename Base>\r
191         std::vector<safe_ptr<Base>> create_consumers(const boost::property_tree::wptree& pt)\r
192         {\r
193                 std::vector<safe_ptr<Base>> consumers;\r
194 \r
195                 create_consumers(pt, [&] (const safe_ptr<core::frame_consumer>& consumer)\r
196                 {\r
197                         consumers.push_back(dynamic_pointer_cast<Base>(consumer));\r
198                 });\r
199 \r
200                 return consumers;\r
201         }\r
202 \r
203         template<class Func>\r
204         void create_consumers(const boost::property_tree::wptree& pt, const Func& on_consumer)\r
205         {\r
206                 BOOST_FOREACH(auto& xml_consumer, pt)\r
207                 {\r
208                         try\r
209                         {\r
210                                 auto name = xml_consumer.first;\r
211 \r
212                                 if (name == L"screen")\r
213                                         on_consumer(ogl::create_consumer(xml_consumer.second));\r
214                                 else if (name == L"bluefish")                                   \r
215                                         on_consumer(bluefish::create_consumer(xml_consumer.second));                                    \r
216                                 else if (name == L"decklink")                                   \r
217                                         on_consumer(decklink::create_consumer(xml_consumer.second));                            \r
218                                 else if (name == L"blocking-decklink")\r
219                                         on_consumer(decklink::create_blocking_consumer(xml_consumer.second));                           \r
220                                 else if (name == L"file" || name == L"stream")                                  \r
221                                         on_consumer(ffmpeg::create_consumer(xml_consumer.second));                                              \r
222                                 else if (name == L"system-audio")\r
223                                         on_consumer(portaudio::create_consumer());\r
224                                 else if (name == L"synchronizing")\r
225                                         on_consumer(make_safe<core::synchronizing_consumer>(\r
226                                                         create_consumers<core::frame_consumer>(\r
227                                                                         xml_consumer.second)));\r
228                                 else if (name != L"<xmlcomment>")\r
229                                         CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
230                         }\r
231                         catch(...)\r
232                         {\r
233                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
234                         }\r
235                 }\r
236         }\r
237                 \r
238         void setup_controllers(const boost::property_tree::wptree& pt)\r
239         {               \r
240                 using boost::property_tree::wptree;\r
241                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
242                 {\r
243                         try\r
244                         {\r
245                                 auto name = xml_controller.first;\r
246                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
247 \r
248                                 if(name == L"tcp")\r
249                                 {                                       \r
250                                         unsigned int port = xml_controller.second.get(L"port", 5250);\r
251                                         auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
252                                         asyncbootstrapper->Start();\r
253                                         async_servers_.push_back(asyncbootstrapper);\r
254 \r
255                                         if (!primary_amcp_server_ && boost::iequals(protocol, L"AMCP"))\r
256                                                 primary_amcp_server_ = asyncbootstrapper;\r
257                                 }\r
258                                 else\r
259                                         CASPAR_LOG(warning) << "Invalid controller: " << widen(name);   \r
260                         }\r
261                         catch(...)\r
262                         {\r
263                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
264                         }\r
265                 }\r
266         }\r
267 \r
268         void setup_osc(const boost::property_tree::wptree& pt)\r
269         {               \r
270                 using boost::property_tree::wptree;\r
271                 using namespace boost::asio::ip;\r
272                 \r
273                 auto default_port =\r
274                                 pt.get<unsigned short>(L"configuration.osc.default-port", 6250);\r
275                 auto predefined_clients =\r
276                                 pt.get_child_optional(L"configuration.osc.predefined-clients");\r
277 \r
278                 if (predefined_clients)\r
279                 {\r
280                         BOOST_FOREACH(auto& predefined_client, *predefined_clients)\r
281                         {\r
282                                 const auto address =\r
283                                                 predefined_client.second.get<std::wstring>(L"address");\r
284                                 const auto port =\r
285                                                 predefined_client.second.get<unsigned short>(L"port");\r
286                                 predefined_osc_subscriptions_.push_back(\r
287                                                 osc_client_.get_subscription_token(udp::endpoint(\r
288                                                                 address_v4::from_string(narrow(address)),\r
289                                                                 port)));\r
290                         }\r
291                 }\r
292 \r
293                 if (primary_amcp_server_)\r
294                         primary_amcp_server_->add_lifecycle_factory(\r
295                                         [=] (const std::string& ipv4_address)\r
296                                                         -> std::shared_ptr<void>\r
297                                         {\r
298                                                 using namespace boost::asio::ip;\r
299 \r
300                                                 return osc_client_.get_subscription_token(\r
301                                                                 udp::endpoint(\r
302                                                                                 address_v4::from_string(ipv4_address),\r
303                                                                                 default_port));\r
304                                         });\r
305         }\r
306 \r
307         void setup_thumbnail_generation(const boost::property_tree::wptree& pt)\r
308         {\r
309                 if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))\r
310                         return;\r
311 \r
312                 auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);\r
313 \r
314                 polling_filesystem_monitor_factory monitor_factory(\r
315                                 io_service_manager_.service(),\r
316                                 scan_interval_millis);\r
317                 thumbnail_generator_.reset(new thumbnail_generator(\r
318                                 monitor_factory, \r
319                                 env::media_folder(),\r
320                                 env::thumbnails_folder(),\r
321                                 pt.get(L"configuration.thumbnails.width", 256),\r
322                                 pt.get(L"configuration.thumbnails.height", 144),\r
323                                 core::video_format_desc::get(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),\r
324                                 ogl_,\r
325                                 pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),\r
326                                 &image::write_cropped_png));\r
327 \r
328                 CASPAR_LOG(info) << L"Initialized thumbnail generator.";\r
329         }\r
330 \r
331         safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
332         {\r
333                 if(boost::iequals(name, L"AMCP"))\r
334                         return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, shutdown_server_now_);\r
335                 else if(boost::iequals(name, L"CII"))\r
336                         return make_safe<cii::CIIProtocolStrategy>(channels_);\r
337                 else if(boost::iequals(name, L"CLOCK"))\r
338                         //return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
339                         return make_safe<IO::stateful_protocol_strategy_wrapper>([=]\r
340                         {\r
341                                 return std::make_shared<CLK::CLKProtocolStrategy>(channels_);\r
342                         });\r
343                 \r
344                 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(narrow(name)) << msg_info("Invalid protocol"));\r
345         }\r
346 };\r
347 \r
348 server::server(boost::promise<bool>& shutdown_server_now) : impl_(new implementation(shutdown_server_now)){}\r
349 \r
350 const std::vector<safe_ptr<video_channel>> server::get_channels() const\r
351 {\r
352         return impl_->channels_;\r
353 }\r
354 \r
355 std::shared_ptr<thumbnail_generator> server::get_thumbnail_generator() const\r
356 {\r
357         return impl_->thumbnail_generator_;\r
358 }\r
359 \r
360 core::monitor::source& server::monitor_output()\r
361 {\r
362         return impl_->monitor_subject_;\r
363 }\r
364 \r
365 }