-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG.\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
-\r
-#include "server.h"\r
-\r
-#include <common/env.h>\r
-#include <common/exception/exceptions.h>\r
-#include <common/utility/string.h>\r
-\r
-#include <core/channel.h>\r
-\r
-#include <modules/bluefish/bluefish.h>\r
-#include <modules/decklink/decklink.h>\r
-#include <modules/ffmpeg/ffmpeg.h>\r
-#include <modules/flash/flash.h>\r
-#include <modules/oal/oal.h>\r
-#include <modules/ogl/ogl.h>\r
-#include <modules/silverlight/silverlight.h>\r
-#include <modules/image/image.h>\r
-\r
-#include <modules/oal/consumer/oal_consumer.h>\r
-#include <modules/bluefish/consumer/bluefish_consumer.h>\r
-#include <modules/decklink/consumer/decklink_consumer.h>\r
-#include <modules/ogl/consumer/ogl_consumer.h>\r
-#include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
-\r
-#include <protocol/amcp/AMCPProtocolStrategy.h>\r
-#include <protocol/cii/CIIProtocolStrategy.h>\r
-#include <protocol/CLK/CLKProtocolStrategy.h>\r
-#include <protocol/util/AsyncEventServer.h>\r
-\r
-#include <boost/algorithm/string.hpp>\r
-#include <boost/lexical_cast.hpp>\r
-#include <boost/filesystem.hpp>\r
-#include <boost/foreach.hpp>\r
-#include <boost/property_tree/ptree.hpp>\r
-#include <boost/property_tree/xml_parser.hpp>\r
-\r
-namespace caspar {\r
-\r
-using namespace core;\r
-using namespace protocol;\r
-\r
-struct server::implementation : boost::noncopyable\r
-{\r
- std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_; \r
- std::vector<safe_ptr<channel>> channels_;\r
-\r
- implementation() \r
- { \r
- init_ffmpeg();\r
- init_bluefish();\r
- init_decklink();\r
- init_flash();\r
- init_oal();\r
- init_ogl();\r
- init_silverlight();\r
- init_image();\r
-\r
- setup_channels(env::properties());\r
- setup_controllers(env::properties());\r
- }\r
-\r
- ~implementation()\r
- { \r
- async_servers_.clear();\r
- channels_.clear();\r
- }\r
- \r
- void setup_channels(const boost::property_tree::ptree& pt)\r
- { \r
- using boost::property_tree::ptree;\r
- BOOST_FOREACH(auto& xml_channel, pt.get_child("configuration.channels"))\r
- { \r
- auto format_desc = video_format_desc::get(widen(xml_channel.second.get("videomode", "PAL"))); \r
- if(format_desc.format == video_format::invalid)\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid videomode."));\r
- \r
- channels_.push_back(channel(channels_.size(), format_desc));\r
- \r
- int index = 0;\r
- BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child("consumers"))\r
- {\r
- try\r
- {\r
- const std::string name = xml_consumer.first;\r
- if(name == "ogl")\r
- channels_.back()->consumer()->add(index++, create_ogl_consumer(xml_consumer.second)); \r
- else if(name == "bluefish") \r
- channels_.back()->consumer()->add(index++, create_bluefish_consumer(xml_consumer.second)); \r
- else if(name == "decklink") \r
- channels_.back()->consumer()->add(index++, create_decklink_consumer(xml_consumer.second)); \r
- else if(name == "audio")\r
- channels_.back()->consumer()->add(index++, oal_consumer()); \r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
- } \r
- }\r
- }\r
- \r
- void setup_controllers(const boost::property_tree::ptree& pt)\r
- { \r
- using boost::property_tree::ptree;\r
- BOOST_FOREACH(auto& xml_controller, pt.get_child("configuration.controllers"))\r
- {\r
- try\r
- {\r
- std::string name = xml_controller.first;\r
- std::string protocol = xml_controller.second.get<std::string>("protocol"); \r
-\r
- if(name == "tcp")\r
- { \r
- unsigned int port = xml_controller.second.get("port", 5250);\r
- auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
- asyncbootstrapper->Start();\r
- async_servers_.push_back(asyncbootstrapper);\r
- }\r
- else\r
- BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info(name) << msg_info("Invalid controller."));\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
- }\r
- }\r
-\r
- safe_ptr<IO::IProtocolStrategy> create_protocol(const std::string& name) const\r
- {\r
- if(name == "AMCP")\r
- return make_safe<amcp::AMCPProtocolStrategy>(channels_);\r
- else if(name == "CII")\r
- return make_safe<cii::CIIProtocolStrategy>(channels_);\r
- else if(name == "CLOCK")\r
- return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
- \r
- BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(name) << msg_info("Invalid protocol"));\r
- }\r
-};\r
-\r
-server::server() : impl_(new implementation()){}\r
-\r
-const std::vector<safe_ptr<channel>> server::get_channels() const\r
-{\r
- return impl_->channels_;\r
-}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+#include "stdafx.h"
+
+#include "server.h"
+
+#include <accelerator/accelerator.h>
+
+#include <common/env.h>
+#include <common/except.h>
+#include <common/utf.h>
+#include <common/memory.h>
+#include <common/polling_filesystem_monitor.h>
+#include <common/diagnostics/graph.h>
+
+#include <core/video_channel.h>
+#include <core/video_format.h>
+#include <core/producer/stage.h>
+#include <core/producer/frame_producer.h>
+#include <core/producer/scene/scene_producer.h>
+#include <core/producer/scene/xml_scene_producer.h>
+#include <core/producer/text/text_producer.h>
+#include <core/consumer/output.h>
+#include <core/thumbnail_generator.h>
+
+#include <modules/bluefish/bluefish.h>
+#include <modules/decklink/decklink.h>
+#include <modules/ffmpeg/ffmpeg.h>
+#include <modules/flash/flash.h>
+#include <modules/oal/oal.h>
+#include <modules/screen/screen.h>
+#include <modules/image/image.h>
+#include <modules/image/consumer/image_consumer.h>
+#include <modules/psd/psd_scene_producer.h>
+
+#include <modules/oal/consumer/oal_consumer.h>
+#include <modules/bluefish/consumer/bluefish_consumer.h>
+#include <modules/decklink/consumer/decklink_consumer.h>
+#include <modules/screen/consumer/screen_consumer.h>
+#include <modules/ffmpeg/consumer/ffmpeg_consumer.h>
+
+#include <protocol/asio/io_service_manager.h>
+#include <protocol/amcp/AMCPProtocolStrategy.h>
+#include <protocol/cii/CIIProtocolStrategy.h>
+#include <protocol/CLK/CLKProtocolStrategy.h>
+#include <protocol/util/AsyncEventServer.h>
+#include <protocol/util/strategy_adapters.h>
+#include <protocol/osc/client.h>
+
+#include <boost/algorithm/string.hpp>
+#include <boost/thread.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/xml_parser.hpp>
+
+#include <future>
+
+namespace caspar {
+
+using namespace core;
+using namespace protocol;
+
+struct server::impl : boost::noncopyable
+{
+ protocol::asio::io_service_manager io_service_manager_;
+ spl::shared_ptr<monitor::subject> monitor_subject_;
+ accelerator::accelerator accelerator_;
+ std::vector<spl::shared_ptr<IO::AsyncEventServer>> async_servers_;
+ std::shared_ptr<IO::AsyncEventServer> primary_amcp_server_;
+ osc::client osc_client_;
+ std::vector<std::shared_ptr<void>> predefined_osc_subscriptions_;
+ std::vector<spl::shared_ptr<video_channel>> channels_;
+ std::shared_ptr<thumbnail_generator> thumbnail_generator_;
+ std::promise<bool>& shutdown_server_now_;
+
+ explicit impl(std::promise<bool>& shutdown_server_now)
+ : accelerator_(env::properties().get(L"configuration.accelerator", L"auto"))
+ , osc_client_(io_service_manager_.service())
+ , shutdown_server_now_(shutdown_server_now)
+ {
+
+ ffmpeg::init();
+ CASPAR_LOG(info) << L"Initialized ffmpeg module.";
+
+ bluefish::init();
+ CASPAR_LOG(info) << L"Initialized bluefish module.";
+
+ decklink::init();
+ CASPAR_LOG(info) << L"Initialized decklink module.";
+
+ oal::init();
+ CASPAR_LOG(info) << L"Initialized oal module.";
+
+ screen::init();
+ CASPAR_LOG(info) << L"Initialized ogl module.";
+
+ image::init();
+ CASPAR_LOG(info) << L"Initialized image module.";
+
+ flash::init();
+ CASPAR_LOG(info) << L"Initialized flash module.";
+
+ psd::init();
+ CASPAR_LOG(info) << L"Initialized psd module.";
+
+ core::text::init();
+
+ register_producer_factory(&core::scene::create_dummy_scene_producer);
+ register_producer_factory(&core::scene::create_xml_scene_producer);
+
+ setup_channels(env::properties());
+ CASPAR_LOG(info) << L"Initialized channels.";
+
+ setup_thumbnail_generation(env::properties());
+ CASPAR_LOG(info) << L"Initialized thumbnail generator.";
+
+ setup_controllers(env::properties());
+ CASPAR_LOG(info) << L"Initialized controllers.";
+
+ setup_osc(env::properties());
+ CASPAR_LOG(info) << L"Initialized osc.";
+ }
+
+ ~impl()
+ {
+ thumbnail_generator_.reset();
+ primary_amcp_server_.reset();
+ async_servers_.clear();
+ channels_.clear();
+
+ boost::this_thread::sleep(boost::posix_time::milliseconds(500));
+ //Sleep(500); // HACK: Wait for asynchronous destruction of producers and consumers.
+
+ image::uninit();
+ ffmpeg::uninit();
+ diagnostics::shutdown();
+ }
+
+ void setup_channels(const boost::property_tree::wptree& pt)
+ {
+ using boost::property_tree::wptree;
+ for (auto& xml_channel : pt.get_child(L"configuration.channels"))
+ {
+ auto format_desc = video_format_desc(xml_channel.second.get(L"video-mode", L"PAL"));
+ if(format_desc.format == video_format::invalid)
+ CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));
+
+ auto channel = spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), format_desc, accelerator_.create_image_mixer());
+
+ for (auto& xml_consumer : xml_channel.second.get_child(L"consumers"))
+ {
+ try
+ {
+ auto name = xml_consumer.first;
+ if(name == L"screen")
+ channel->output().add(caspar::screen::create_consumer(xml_consumer.second, &channel->stage()));
+ else if(name == L"bluefish")
+ channel->output().add(bluefish::create_consumer(xml_consumer.second));
+ else if(name == L"decklink")
+ channel->output().add(decklink::create_consumer(xml_consumer.second));
+ else if(name == L"file")
+ channel->output().add(ffmpeg::create_consumer(xml_consumer.second));
+ else if(name == L"system-audio")
+ channel->output().add(oal::create_consumer());
+ else if(name != L"<xmlcomment>")
+ CASPAR_LOG(warning) << "Invalid consumer: " << name;
+ }
+ catch(...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ }
+ }
+
+ channel->monitor_output().attach_parent(monitor_subject_);
+ channels_.push_back(channel);
+ }
+
+ // Dummy diagnostics channel
+ if(env::properties().get(L"configuration.channel-grid", false))
+ channels_.push_back(spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), core::video_format_desc(core::video_format::x576p2500), accelerator_.create_image_mixer()));
+ }
+
+ void setup_osc(const boost::property_tree::wptree& pt)
+ {
+ using boost::property_tree::wptree;
+ using namespace boost::asio::ip;
+
+ monitor_subject_->attach_parent(osc_client_.sink());
+
+ auto default_port =
+ pt.get<unsigned short>(L"configuration.osc.default-port", 6250);
+ auto predefined_clients =
+ pt.get_child_optional(L"configuration.osc.predefined-clients");
+
+ if (predefined_clients)
+ {
+ for (auto& predefined_client : *predefined_clients)
+ {
+ const auto address =
+ predefined_client.second.get<std::wstring>(L"address");
+ const auto port =
+ predefined_client.second.get<unsigned short>(L"port");
+ predefined_osc_subscriptions_.push_back(
+ osc_client_.get_subscription_token(udp::endpoint(
+ address_v4::from_string(u8(address)),
+ port)));
+ }
+ }
+
+ if (primary_amcp_server_)
+ primary_amcp_server_->add_client_lifecycle_object_factory(
+ [=] (const std::string& ipv4_address)
+ -> std::pair<std::wstring, std::shared_ptr<void>>
+ {
+ using namespace boost::asio::ip;
+
+ return std::make_pair(
+ std::wstring(L"osc_subscribe"),
+ osc_client_.get_subscription_token(
+ udp::endpoint(
+ address_v4::from_string(
+ ipv4_address),
+ default_port)));
+ });
+ }
+
+ void setup_thumbnail_generation(const boost::property_tree::wptree& pt)
+ {
+ if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))
+ return;
+
+ auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);
+
+ polling_filesystem_monitor_factory monitor_factory(scan_interval_millis);
+ thumbnail_generator_.reset(new thumbnail_generator(
+ monitor_factory,
+ env::media_folder(),
+ env::thumbnails_folder(),
+ pt.get(L"configuration.thumbnails.width", 256),
+ pt.get(L"configuration.thumbnails.height", 144),
+ core::video_format_desc(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),
+ accelerator_.create_image_mixer(),
+ pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),
+ &image::write_cropped_png));
+
+ CASPAR_LOG(info) << L"Initialized thumbnail generator.";
+ }
+
+ void setup_controllers(const boost::property_tree::wptree& pt)
+ {
+ using boost::property_tree::wptree;
+ for (auto& xml_controller : pt.get_child(L"configuration.controllers"))
+ {
+ try
+ {
+ auto name = xml_controller.first;
+ auto protocol = xml_controller.second.get<std::wstring>(L"protocol");
+
+ if(name == L"tcp")
+ {
+ unsigned int port = xml_controller.second.get(L"port", 5250);
+ auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(create_protocol(protocol), port);
+ async_servers_.push_back(asyncbootstrapper);
+
+ if (!primary_amcp_server_ && boost::iequals(protocol, L"AMCP"))
+ primary_amcp_server_ = asyncbootstrapper;
+ }
+ else
+ CASPAR_LOG(warning) << "Invalid controller: " << name;
+ }
+ catch(...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ }
+ }
+ }
+
+ IO::protocol_strategy_factory<char>::ptr create_protocol(const std::wstring& name) const
+ {
+ using namespace IO;
+
+ if(boost::iequals(name, L"AMCP"))
+ return wrap_legacy_protocol("\r\n", spl::make_shared<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, shutdown_server_now_));
+ else if(boost::iequals(name, L"CII"))
+ return wrap_legacy_protocol("\r\n", spl::make_shared<cii::CIIProtocolStrategy>(channels_));
+ else if(boost::iequals(name, L"CLOCK"))
+ return spl::make_shared<to_unicode_adapter_factory>(
+ "ISO-8859-1",
+ spl::make_shared<CLK::clk_protocol_strategy_factory>(channels_));
+
+ CASPAR_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol"));
+ }
+
+};
+
+server::server(std::promise<bool>& shutdown_server_now) : impl_(new impl(shutdown_server_now)){}
+
+const std::vector<spl::shared_ptr<video_channel>> server::channels() const
+{
+ return impl_->channels_;
+}
+std::shared_ptr<core::thumbnail_generator> server::get_thumbnail_generator() const {return impl_->thumbnail_generator_; }
+core::monitor::subject& server::monitor_output() { return *impl_->monitor_subject_; }
+
}
\ No newline at end of file