]> git.sesse.net Git - casparcg/blob - shell/server.cpp
OSC for channel_grid channel as well
[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 #include <core/producer/media_info/media_info.h>\r
41 #include <core/producer/media_info/media_info_repository.h>\r
42 #include <core/producer/media_info/in_memory_media_info_repository.h>\r
43 \r
44 #include <modules/bluefish/bluefish.h>\r
45 #include <modules/decklink/decklink.h>\r
46 #include <modules/ffmpeg/ffmpeg.h>\r
47 #include <modules/flash/flash.h>\r
48 #include <modules/html/html.h>\r
49 #include <modules/oal/oal.h>\r
50 #include <modules/ogl/ogl.h>\r
51 #include <modules/newtek/newtek.h>\r
52 #include <modules/image/image.h>\r
53 #include <modules/image/consumer/image_consumer.h>\r
54 \r
55 #include <modules/oal/consumer/oal_consumer.h>\r
56 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
57 #include <modules/newtek/consumer/newtek_ivga_consumer.h>\r
58 #include <modules/decklink/consumer/decklink_consumer.h>\r
59 #include <modules/decklink/consumer/blocking_decklink_consumer.h>\r
60 #include <modules/ogl/consumer/ogl_consumer.h>\r
61 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
62 \r
63 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
64 #include <protocol/cii/CIIProtocolStrategy.h>\r
65 #include <protocol/CLK/CLKProtocolStrategy.h>\r
66 #include <protocol/util/AsyncEventServer.h>\r
67 #include <protocol/util/stateful_protocol_strategy_wrapper.h>\r
68 #include <protocol/osc/client.h>\r
69 \r
70 #include <boost/algorithm/string.hpp>\r
71 #include <boost/lexical_cast.hpp>\r
72 #include <boost/filesystem.hpp>\r
73 #include <boost/foreach.hpp>\r
74 #include <boost/property_tree/ptree.hpp>\r
75 #include <boost/property_tree/xml_parser.hpp>\r
76 #include <boost/asio.hpp>\r
77 \r
78 #include <tbb/atomic.h>\r
79 \r
80 namespace caspar {\r
81 \r
82 using namespace core;\r
83 using namespace protocol;\r
84 \r
85 std::shared_ptr<boost::asio::io_service> create_running_io_service()\r
86 {\r
87         auto service = std::make_shared<boost::asio::io_service>();\r
88         // To keep the io_service::run() running although no pending async\r
89         // operations are posted.\r
90         auto work = std::make_shared<boost::asio::io_service::work>(*service);\r
91         auto thread = std::make_shared<boost::thread>([service]\r
92         {\r
93                 win32_exception::ensure_handler_installed_for_thread("asio-thread");\r
94 \r
95                 service->run();\r
96         });\r
97 \r
98         return std::shared_ptr<boost::asio::io_service>(\r
99                         service.get(),\r
100                         [service, work, thread] (void*) mutable\r
101                         {\r
102                                 work.reset();\r
103                                 service->stop();\r
104                                 thread->join();\r
105                         });\r
106 }\r
107 \r
108 struct server::implementation : boost::noncopyable\r
109 {\r
110         std::shared_ptr<boost::asio::io_service>        io_service_;\r
111         safe_ptr<core::monitor::subject>                        monitor_subject_;\r
112         boost::promise<bool>&                                           shutdown_server_now_;\r
113         safe_ptr<ogl_device>                                            ogl_;\r
114         std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;     \r
115         std::shared_ptr<IO::AsyncEventServer>           primary_amcp_server_;\r
116         osc::client                                                                     osc_client_;\r
117         std::vector<std::shared_ptr<void>>                      predefined_osc_subscriptions_;\r
118         std::vector<safe_ptr<video_channel>>            channels_;\r
119         safe_ptr<media_info_repository>                         media_info_repo_;\r
120         boost::thread                                                           initial_media_info_thread_;\r
121         tbb::atomic<bool>                                                       running_;\r
122         std::shared_ptr<thumbnail_generator>            thumbnail_generator_;\r
123 \r
124         implementation(boost::promise<bool>& shutdown_server_now)\r
125                 : io_service_(create_running_io_service())\r
126                 , shutdown_server_now_(shutdown_server_now)\r
127                 , ogl_(ogl_device::create())\r
128                 , osc_client_(io_service_)\r
129                 , media_info_repo_(create_in_memory_media_info_repository())\r
130         {\r
131                 running_ = true;\r
132                 setup_audio(env::properties());\r
133                 \r
134                 html::init();\r
135                 CASPAR_LOG(info) << L"Initialized html module.";\r
136 \r
137                 ffmpeg::init(media_info_repo_);\r
138                 CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
139                                                           \r
140                 bluefish::init();         \r
141                 CASPAR_LOG(info) << L"Initialized bluefish module.";\r
142                                                           \r
143                 decklink::init();         \r
144                 CASPAR_LOG(info) << L"Initialized decklink module.";\r
145 \r
146                 oal::init();\r
147                 CASPAR_LOG(info) << L"Initialized oal module.";\r
148                                                           \r
149                 newtek::init();\r
150                 CASPAR_LOG(info) << L"Initialized newtek module.";\r
151 \r
152                 ogl::init();              \r
153                 CASPAR_LOG(info) << L"Initialized ogl module.";\r
154 \r
155                 flash::init();            \r
156                 CASPAR_LOG(info) << L"Initialized flash module.";\r
157 \r
158                 image::init();            \r
159                 CASPAR_LOG(info) << L"Initialized image module.";\r
160 \r
161                 setup_channels(env::properties());\r
162                 CASPAR_LOG(info) << L"Initialized channels.";\r
163 \r
164                 setup_thumbnail_generation(env::properties());\r
165 \r
166                 setup_controllers(env::properties());\r
167                 CASPAR_LOG(info) << L"Initialized controllers.";\r
168 \r
169                 setup_osc(env::properties());\r
170                 CASPAR_LOG(info) << L"Initialized osc.";\r
171 \r
172                 start_initial_media_info_scan();\r
173                 CASPAR_LOG(info) << L"Started initial media information retrieval.";\r
174         }\r
175 \r
176         ~implementation()\r
177         {\r
178                 running_ = false;\r
179                 initial_media_info_thread_.join();\r
180                 thumbnail_generator_.reset();\r
181                 primary_amcp_server_.reset();\r
182                 async_servers_.clear();\r
183                 destroy_producers_synchronously();\r
184                 channels_.clear();\r
185 \r
186                 html::uninit();\r
187                 ffmpeg::uninit();\r
188         }\r
189 \r
190         void setup_audio(const boost::property_tree::wptree& pt)\r
191         {\r
192                 register_default_channel_layouts(default_channel_layout_repository());\r
193                 register_default_mix_configs(default_mix_config_repository());\r
194 \r
195                 auto channel_layouts =\r
196                         pt.get_child_optional(L"configuration.audio.channel-layouts");\r
197                 auto mix_configs =\r
198                         pt.get_child_optional(L"configuration.audio.mix-configs");\r
199 \r
200                 if (channel_layouts)\r
201                         parse_channel_layouts(\r
202                                         default_channel_layout_repository(), *channel_layouts);\r
203 \r
204                 if (mix_configs)\r
205                         parse_mix_configs(\r
206                                         default_mix_config_repository(), *mix_configs);\r
207         }\r
208                                 \r
209         void setup_channels(const boost::property_tree::wptree& pt)\r
210         {   \r
211                 using boost::property_tree::wptree;\r
212                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
213                 {               \r
214                         auto format_desc = video_format_desc::get(widen(xml_channel.second.get(L"video-mode", L"PAL")));                \r
215                         if(format_desc.format == video_format::invalid)\r
216                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
217                         auto audio_channel_layout = default_channel_layout_repository().get_by_name(\r
218                                         boost::to_upper_copy(xml_channel.second.get(L"channel-layout", L"STEREO")));\r
219                         \r
220                         channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_, audio_channel_layout));\r
221                         \r
222                         channels_.back()->monitor_output().attach_parent(monitor_subject_);\r
223                         channels_.back()->mixer()->set_straight_alpha_output(\r
224                                         xml_channel.second.get(L"straight-alpha-output", false));\r
225 \r
226                         create_consumers(\r
227                                 xml_channel.second.get_child(L"consumers"),\r
228                                 [&] (const safe_ptr<core::frame_consumer>& consumer)\r
229                                 {\r
230                                         channels_.back()->output()->add(consumer);\r
231                                 });\r
232                 }\r
233 \r
234                 // Dummy diagnostics channel\r
235                 if(env::properties().get(L"configuration.channel-grid", false))\r
236                 {\r
237                         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
238                         channels_.back()->monitor_output().attach_parent(monitor_subject_);\r
239                 }\r
240         }\r
241 \r
242         template<typename Base>\r
243         std::vector<safe_ptr<Base>> create_consumers(const boost::property_tree::wptree& pt)\r
244         {\r
245                 std::vector<safe_ptr<Base>> consumers;\r
246 \r
247                 create_consumers(pt, [&] (const safe_ptr<core::frame_consumer>& consumer)\r
248                 {\r
249                         consumers.push_back(dynamic_pointer_cast<Base>(consumer));\r
250                 });\r
251 \r
252                 return consumers;\r
253         }\r
254 \r
255         template<class Func>\r
256         void create_consumers(const boost::property_tree::wptree& pt, const Func& on_consumer)\r
257         {\r
258                 BOOST_FOREACH(auto& xml_consumer, pt)\r
259                 {\r
260                         try\r
261                         {\r
262                                 auto name = xml_consumer.first;\r
263 \r
264                                 if (name == L"screen")\r
265                                         on_consumer(ogl::create_consumer(xml_consumer.second));\r
266                                 else if (name == L"bluefish")                                   \r
267                                         on_consumer(bluefish::create_consumer(xml_consumer.second));                                    \r
268                                 else if (name == L"decklink")                                   \r
269                                         on_consumer(decklink::create_consumer(xml_consumer.second));                            \r
270                                 else if (name == L"newtek-ivga")                                        \r
271                                         on_consumer(newtek::create_ivga_consumer(xml_consumer.second));                 \r
272                                 else if (name == L"blocking-decklink")\r
273                                         on_consumer(decklink::create_blocking_consumer(xml_consumer.second));                           \r
274                                 else if (name == L"file" || name == L"stream")                                  \r
275                                         on_consumer(ffmpeg::create_consumer(xml_consumer.second));                                              \r
276                                 else if (name == L"system-audio")\r
277                                         on_consumer(oal::create_consumer());\r
278                                 else if (name == L"synchronizing")\r
279                                         on_consumer(make_safe<core::synchronizing_consumer>(\r
280                                                         create_consumers<core::frame_consumer>(\r
281                                                                         xml_consumer.second)));\r
282                                 else if (name != L"<xmlcomment>")\r
283                                         CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
284                         }\r
285                         catch(...)\r
286                         {\r
287                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
288                         }\r
289                 }\r
290         }\r
291                 \r
292         void setup_controllers(const boost::property_tree::wptree& pt)\r
293         {               \r
294                 using boost::property_tree::wptree;\r
295                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
296                 {\r
297                         try\r
298                         {\r
299                                 auto name = xml_controller.first;\r
300                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
301 \r
302                                 if(name == L"tcp")\r
303                                 {                                       \r
304                                         unsigned int port = xml_controller.second.get(L"port", 5250);\r
305                                         auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
306                                         asyncbootstrapper->Start();\r
307                                         async_servers_.push_back(asyncbootstrapper);\r
308 \r
309                                         if (!primary_amcp_server_ && boost::iequals(protocol, L"AMCP"))\r
310                                                 primary_amcp_server_ = asyncbootstrapper;\r
311                                 }\r
312                                 else\r
313                                         CASPAR_LOG(warning) << "Invalid controller: " << widen(name);   \r
314                         }\r
315                         catch(...)\r
316                         {\r
317                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
318                         }\r
319                 }\r
320         }\r
321 \r
322         void setup_osc(const boost::property_tree::wptree& pt)\r
323         {               \r
324                 using boost::property_tree::wptree;\r
325                 using namespace boost::asio::ip;\r
326 \r
327                 monitor_subject_->attach_parent(osc_client_.sink());\r
328                 \r
329                 auto default_port =\r
330                                 pt.get<unsigned short>(L"configuration.osc.default-port", 6250);\r
331                 auto predefined_clients =\r
332                                 pt.get_child_optional(L"configuration.osc.predefined-clients");\r
333 \r
334                 if (predefined_clients)\r
335                 {\r
336                         BOOST_FOREACH(auto& predefined_client, *predefined_clients)\r
337                         {\r
338                                 const auto address =\r
339                                                 predefined_client.second.get<std::wstring>(L"address");\r
340                                 const auto port =\r
341                                                 predefined_client.second.get<unsigned short>(L"port");\r
342                                 predefined_osc_subscriptions_.push_back(\r
343                                                 osc_client_.get_subscription_token(udp::endpoint(\r
344                                                                 address_v4::from_string(narrow(address)),\r
345                                                                 port)));\r
346                         }\r
347                 }\r
348 \r
349                 if (primary_amcp_server_)\r
350                         primary_amcp_server_->add_lifecycle_factory(\r
351                                         [=] (const std::string& ipv4_address)\r
352                                                         -> std::shared_ptr<void>\r
353                                         {\r
354                                                 using namespace boost::asio::ip;\r
355 \r
356                                                 return osc_client_.get_subscription_token(\r
357                                                                 udp::endpoint(\r
358                                                                                 address_v4::from_string(ipv4_address),\r
359                                                                                 default_port));\r
360                                         });\r
361         }\r
362 \r
363         void setup_thumbnail_generation(const boost::property_tree::wptree& pt)\r
364         {\r
365                 if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))\r
366                         return;\r
367 \r
368                 auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);\r
369 \r
370                 polling_filesystem_monitor_factory monitor_factory(\r
371                                 io_service_, scan_interval_millis);\r
372                 thumbnail_generator_.reset(new thumbnail_generator(\r
373                                 monitor_factory, \r
374                                 env::media_folder(),\r
375                                 env::thumbnails_folder(),\r
376                                 pt.get(L"configuration.thumbnails.width", 256),\r
377                                 pt.get(L"configuration.thumbnails.height", 144),\r
378                                 core::video_format_desc::get(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),\r
379                                 ogl_,\r
380                                 pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),\r
381                                 &image::write_cropped_png,\r
382                                 media_info_repo_));\r
383 \r
384                 CASPAR_LOG(info) << L"Initialized thumbnail generator.";\r
385         }\r
386 \r
387         safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
388         {\r
389                 if(boost::iequals(name, L"AMCP"))\r
390                         return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, media_info_repo_, shutdown_server_now_);\r
391                 else if(boost::iequals(name, L"CII"))\r
392                         return make_safe<cii::CIIProtocolStrategy>(channels_);\r
393                 else if(boost::iequals(name, L"CLOCK"))\r
394                         //return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
395                         return make_safe<IO::stateful_protocol_strategy_wrapper>([=]\r
396                         {\r
397                                 return std::make_shared<CLK::CLKProtocolStrategy>(channels_);\r
398                         });\r
399                 \r
400                 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(narrow(name)) << msg_info("Invalid protocol"));\r
401         }\r
402 \r
403         void start_initial_media_info_scan()\r
404         {\r
405                 initial_media_info_thread_ = boost::thread([this]\r
406                 {\r
407                         for (boost::filesystem::wrecursive_directory_iterator iter(env::media_folder()), end; iter != end; ++iter)\r
408                         {\r
409                                 if (running_)\r
410                                         media_info_repo_->get(iter->path().file_string());\r
411                                 else\r
412                                 {\r
413                                         CASPAR_LOG(info) << L"Initial media information retrieval aborted.";\r
414                                         return;\r
415                                 }\r
416                         }\r
417 \r
418                         CASPAR_LOG(info) << L"Initial media information retrieval finished.";\r
419                 });\r
420         }\r
421 };\r
422 \r
423 server::server(boost::promise<bool>& shutdown_server_now) : impl_(new implementation(shutdown_server_now)){}\r
424 \r
425 const std::vector<safe_ptr<video_channel>> server::get_channels() const\r
426 {\r
427         return impl_->channels_;\r
428 }\r
429 \r
430 std::shared_ptr<thumbnail_generator> server::get_thumbnail_generator() const\r
431 {\r
432         return impl_->thumbnail_generator_;\r
433 }\r
434 \r
435 safe_ptr<media_info_repository> server::get_media_info_repo() const\r
436 {\r
437         return impl_->media_info_repo_;\r
438 }\r
439 \r
440 core::monitor::subject& server::monitor_output()\r
441 {\r
442         return *impl_->monitor_subject_;\r
443 }\r
444 \r
445 }