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