2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\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
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
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/oal/oal.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
52 #include <modules/oal/consumer/oal_consumer.h>
\r
53 #include <modules/bluefish/consumer/bluefish_consumer.h>
\r
54 #include <modules/newtek/consumer/newtek_ivga_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
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
67 #include <boost/algorithm/string.hpp>
\r
68 #include <boost/lexical_cast.hpp>
\r
69 #include <boost/foreach.hpp>
\r
70 #include <boost/property_tree/ptree.hpp>
\r
71 #include <boost/property_tree/xml_parser.hpp>
\r
72 #include <boost/asio.hpp>
\r
76 using namespace core;
\r
77 using namespace protocol;
\r
79 std::shared_ptr<boost::asio::io_service> create_running_io_service()
\r
81 auto service = std::make_shared<boost::asio::io_service>();
\r
82 // To keep the io_service::run() running although no pending async
\r
83 // operations are posted.
\r
84 auto work = std::make_shared<boost::asio::io_service::work>(*service);
\r
85 auto thread = std::make_shared<boost::thread>([service]
\r
87 win32_exception::ensure_handler_installed_for_thread("asio-thread");
\r
92 return std::shared_ptr<boost::asio::io_service>(
\r
94 [service, work, thread] (void*) mutable
\r
102 struct server::implementation : boost::noncopyable
\r
104 std::shared_ptr<boost::asio::io_service> io_service_;
\r
105 safe_ptr<core::monitor::subject> monitor_subject_;
\r
106 boost::promise<bool>& shutdown_server_now_;
\r
107 safe_ptr<ogl_device> ogl_;
\r
108 std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;
\r
109 std::shared_ptr<IO::AsyncEventServer> primary_amcp_server_;
\r
110 osc::client osc_client_;
\r
111 std::vector<std::shared_ptr<void>> predefined_osc_subscriptions_;
\r
112 std::vector<safe_ptr<video_channel>> channels_;
\r
113 std::shared_ptr<thumbnail_generator> thumbnail_generator_;
\r
115 implementation(boost::promise<bool>& shutdown_server_now)
\r
116 : io_service_(create_running_io_service())
\r
117 , shutdown_server_now_(shutdown_server_now)
\r
118 , ogl_(ogl_device::create())
\r
119 , osc_client_(io_service_)
\r
121 setup_audio(env::properties());
\r
124 CASPAR_LOG(info) << L"Initialized ffmpeg module.";
\r
127 CASPAR_LOG(info) << L"Initialized bluefish module.";
\r
130 CASPAR_LOG(info) << L"Initialized decklink module.";
\r
133 CASPAR_LOG(info) << L"Initialized oal module.";
\r
136 CASPAR_LOG(info) << L"Initialized newtek module.";
\r
139 CASPAR_LOG(info) << L"Initialized ogl module.";
\r
142 CASPAR_LOG(info) << L"Initialized flash module.";
\r
145 CASPAR_LOG(info) << L"Initialized image module.";
\r
147 setup_channels(env::properties());
\r
148 CASPAR_LOG(info) << L"Initialized channels.";
\r
150 setup_thumbnail_generation(env::properties());
\r
152 setup_controllers(env::properties());
\r
153 CASPAR_LOG(info) << L"Initialized controllers.";
\r
155 setup_osc(env::properties());
\r
156 CASPAR_LOG(info) << L"Initialized osc.";
\r
161 thumbnail_generator_.reset();
\r
162 primary_amcp_server_.reset();
\r
163 async_servers_.clear();
\r
164 destroy_producers_synchronously();
\r
170 void setup_audio(const boost::property_tree::wptree& pt)
\r
172 register_default_channel_layouts(default_channel_layout_repository());
\r
173 register_default_mix_configs(default_mix_config_repository());
\r
175 auto channel_layouts =
\r
176 pt.get_child_optional(L"configuration.audio.channel-layouts");
\r
178 pt.get_child_optional(L"configuration.audio.mix-configs");
\r
180 if (channel_layouts)
\r
181 parse_channel_layouts(
\r
182 default_channel_layout_repository(), *channel_layouts);
\r
186 default_mix_config_repository(), *mix_configs);
\r
189 void setup_channels(const boost::property_tree::wptree& pt)
\r
191 using boost::property_tree::wptree;
\r
192 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))
\r
194 auto format_desc = video_format_desc::get(widen(xml_channel.second.get(L"video-mode", L"PAL")));
\r
195 if(format_desc.format == video_format::invalid)
\r
196 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));
\r
197 auto audio_channel_layout = default_channel_layout_repository().get_by_name(
\r
198 boost::to_upper_copy(xml_channel.second.get(L"channel-layout", L"STEREO")));
\r
200 channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_, audio_channel_layout));
\r
202 channels_.back()->monitor_output().attach_parent(monitor_subject_);
\r
203 channels_.back()->mixer()->set_straight_alpha_output(
\r
204 xml_channel.second.get(L"straight-alpha-output", false));
\r
207 xml_channel.second.get_child(L"consumers"),
\r
208 [&] (const safe_ptr<core::frame_consumer>& consumer)
\r
210 channels_.back()->output()->add(consumer);
\r
214 // Dummy diagnostics channel
\r
215 if(env::properties().get(L"configuration.channel-grid", false))
\r
216 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
219 template<typename Base>
\r
220 std::vector<safe_ptr<Base>> create_consumers(const boost::property_tree::wptree& pt)
\r
222 std::vector<safe_ptr<Base>> consumers;
\r
224 create_consumers(pt, [&] (const safe_ptr<core::frame_consumer>& consumer)
\r
226 consumers.push_back(dynamic_pointer_cast<Base>(consumer));
\r
232 template<class Func>
\r
233 void create_consumers(const boost::property_tree::wptree& pt, const Func& on_consumer)
\r
235 BOOST_FOREACH(auto& xml_consumer, pt)
\r
239 auto name = xml_consumer.first;
\r
241 if (name == L"screen")
\r
242 on_consumer(ogl::create_consumer(xml_consumer.second));
\r
243 else if (name == L"bluefish")
\r
244 on_consumer(bluefish::create_consumer(xml_consumer.second));
\r
245 else if (name == L"decklink")
\r
246 on_consumer(decklink::create_consumer(xml_consumer.second));
\r
247 else if (name == L"newtek-ivga")
\r
248 on_consumer(newtek::create_ivga_consumer(xml_consumer.second));
\r
249 else if (name == L"blocking-decklink")
\r
250 on_consumer(decklink::create_blocking_consumer(xml_consumer.second));
\r
251 else if (name == L"file" || name == L"stream")
\r
252 on_consumer(ffmpeg::create_consumer(xml_consumer.second));
\r
253 else if (name == L"system-audio")
\r
254 on_consumer(oal::create_consumer());
\r
255 else if (name == L"synchronizing")
\r
256 on_consumer(make_safe<core::synchronizing_consumer>(
\r
257 create_consumers<core::frame_consumer>(
\r
258 xml_consumer.second)));
\r
259 else if (name != L"<xmlcomment>")
\r
260 CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);
\r
264 CASPAR_LOG_CURRENT_EXCEPTION();
\r
269 void setup_controllers(const boost::property_tree::wptree& pt)
\r
271 using boost::property_tree::wptree;
\r
272 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))
\r
276 auto name = xml_controller.first;
\r
277 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");
\r
281 unsigned int port = xml_controller.second.get(L"port", 5250);
\r
282 auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);
\r
283 asyncbootstrapper->Start();
\r
284 async_servers_.push_back(asyncbootstrapper);
\r
286 if (!primary_amcp_server_ && boost::iequals(protocol, L"AMCP"))
\r
287 primary_amcp_server_ = asyncbootstrapper;
\r
290 CASPAR_LOG(warning) << "Invalid controller: " << widen(name);
\r
294 CASPAR_LOG_CURRENT_EXCEPTION();
\r
299 void setup_osc(const boost::property_tree::wptree& pt)
\r
301 using boost::property_tree::wptree;
\r
302 using namespace boost::asio::ip;
\r
304 monitor_subject_->attach_parent(osc_client_.sink());
\r
306 auto default_port =
\r
307 pt.get<unsigned short>(L"configuration.osc.default-port", 6250);
\r
308 auto predefined_clients =
\r
309 pt.get_child_optional(L"configuration.osc.predefined-clients");
\r
311 if (predefined_clients)
\r
313 BOOST_FOREACH(auto& predefined_client, *predefined_clients)
\r
315 const auto address =
\r
316 predefined_client.second.get<std::wstring>(L"address");
\r
318 predefined_client.second.get<unsigned short>(L"port");
\r
319 predefined_osc_subscriptions_.push_back(
\r
320 osc_client_.get_subscription_token(udp::endpoint(
\r
321 address_v4::from_string(narrow(address)),
\r
326 if (primary_amcp_server_)
\r
327 primary_amcp_server_->add_lifecycle_factory(
\r
328 [=] (const std::string& ipv4_address)
\r
329 -> std::shared_ptr<void>
\r
331 using namespace boost::asio::ip;
\r
333 return osc_client_.get_subscription_token(
\r
335 address_v4::from_string(ipv4_address),
\r
340 void setup_thumbnail_generation(const boost::property_tree::wptree& pt)
\r
342 if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))
\r
345 auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);
\r
347 polling_filesystem_monitor_factory monitor_factory(
\r
348 io_service_, scan_interval_millis);
\r
349 thumbnail_generator_.reset(new thumbnail_generator(
\r
351 env::media_folder(),
\r
352 env::thumbnails_folder(),
\r
353 pt.get(L"configuration.thumbnails.width", 256),
\r
354 pt.get(L"configuration.thumbnails.height", 144),
\r
355 core::video_format_desc::get(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),
\r
357 pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),
\r
358 &image::write_cropped_png));
\r
360 CASPAR_LOG(info) << L"Initialized thumbnail generator.";
\r
363 safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const
\r
365 if(boost::iequals(name, L"AMCP"))
\r
366 return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, shutdown_server_now_);
\r
367 else if(boost::iequals(name, L"CII"))
\r
368 return make_safe<cii::CIIProtocolStrategy>(channels_);
\r
369 else if(boost::iequals(name, L"CLOCK"))
\r
370 //return make_safe<CLK::CLKProtocolStrategy>(channels_);
\r
371 return make_safe<IO::stateful_protocol_strategy_wrapper>([=]
\r
373 return std::make_shared<CLK::CLKProtocolStrategy>(channels_);
\r
376 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(narrow(name)) << msg_info("Invalid protocol"));
\r
380 server::server(boost::promise<bool>& shutdown_server_now) : impl_(new implementation(shutdown_server_now)){}
\r
382 const std::vector<safe_ptr<video_channel>> server::get_channels() const
\r
384 return impl_->channels_;
\r
387 std::shared_ptr<thumbnail_generator> server::get_thumbnail_generator() const
\r
389 return impl_->thumbnail_generator_;
\r
392 core::monitor::subject& server::monitor_output()
\r
394 return *impl_->monitor_subject_;
\r