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