]> git.sesse.net Git - casparcg/blobdiff - shell/server.cpp
[polling_filesystem_monitor] Fixed bug where unallocated memory could be read by...
[casparcg] / shell / server.cpp
index 2da08565a1c8833f8813022900ccf6058182a5ba..69fa7c083c0430e8e50c568f242b7f04e1d65ad2 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\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
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-#include "stdafx.h"\r
-\r
-#include "server.h"\r
-\r
-#include <accelerator/accelerator.h>\r
-\r
-#include <common/env.h>\r
-#include <common/except.h>\r
-#include <common/utf.h>\r
-\r
-#include <core/video_channel.h>\r
-#include <core/video_format.h>\r
-#include <core/producer/stage.h>\r
-#include <core/consumer/output.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/screen/screen.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/screen/consumer/screen_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/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::impl : boost::noncopyable\r
-{\r
-       monitor::basic_subject                                                          event_subject_;\r
-       accelerator::accelerator                                                        accelerator_;\r
-       std::vector<spl::shared_ptr<IO::AsyncEventServer>>      async_servers_; \r
-       std::vector<spl::shared_ptr<video_channel>>                     channels_;\r
-\r
-       impl()          \r
-               : accelerator_(env::properties().get(L"configuration.accelerator", L"auto"))\r
-       {       \r
-\r
-               ffmpeg::init();\r
-               CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
-                                                         \r
-               bluefish::init();         \r
-               CASPAR_LOG(info) << L"Initialized bluefish module.";\r
-                                                         \r
-               decklink::init();         \r
-               CASPAR_LOG(info) << L"Initialized decklink module.";\r
-                                                                                                                 \r
-               oal::init();              \r
-               CASPAR_LOG(info) << L"Initialized oal module.";\r
-                                                         \r
-               screen::init();           \r
-               CASPAR_LOG(info) << L"Initialized ogl module.";\r
-\r
-               image::init();            \r
-               CASPAR_LOG(info) << L"Initialized image module.";\r
-\r
-               flash::init();            \r
-               CASPAR_LOG(info) << L"Initialized flash module.";\r
-\r
-               setup_channels(env::properties());\r
-               CASPAR_LOG(info) << L"Initialized channels.";\r
-\r
-               setup_controllers(env::properties());\r
-               CASPAR_LOG(info) << L"Initialized controllers.";\r
-       }\r
-\r
-       ~impl()\r
-       {               \r
-               async_servers_.clear();\r
-               channels_.clear();\r
-\r
-               Sleep(500); // HACK: Wait for asynchronous destruction of producers and consumers.\r
-\r
-               image::uninit();\r
-               ffmpeg::uninit();\r
-       }\r
-                               \r
-       void setup_channels(const boost::property_tree::wptree& pt)\r
-       {   \r
-               using boost::property_tree::wptree;\r
-               BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
-               {               \r
-                       auto format_desc = video_format_desc(xml_channel.second.get(L"video-mode", L"PAL"));            \r
-                       if(format_desc.format == video_format::invalid)\r
-                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
-                       \r
-                       auto channel = spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), format_desc, accelerator_.create_image_mixer());\r
-                       \r
-                       BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))\r
-                       {\r
-                               try\r
-                               {\r
-                                       auto name = xml_consumer.first;\r
-                                       if(name == L"screen")\r
-                                               channel->output().add(caspar::screen::create_consumer(xml_consumer.second));                                    \r
-                                       else if(name == L"bluefish")                                    \r
-                                               channel->output().add(bluefish::create_consumer(xml_consumer.second));                                  \r
-                                       else if(name == L"decklink")                                    \r
-                                               channel->output().add(decklink::create_consumer(xml_consumer.second));                          \r
-                                       else if(name == L"file")                                        \r
-                                               channel->output().add(ffmpeg::create_consumer(xml_consumer.second));                                            \r
-                                       else if(name == L"system-audio")\r
-                                               channel->output().add(oal::create_consumer());          \r
-                                       else if(name != L"<xmlcomment>")\r
-                                               CASPAR_LOG(warning) << "Invalid consumer: " << name;    \r
-                               }\r
-                               catch(...)\r
-                               {\r
-                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               }\r
-                       }               \r
-\r
-                   channel->subscribe(monitor::observable::observer_ptr(event_subject_));\r
-                       channels_.push_back(channel);\r
-               }\r
-\r
-               // Dummy diagnostics channel\r
-               if(env::properties().get(L"configuration.channel-grid", false))\r
-                       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()));\r
-       }\r
-               \r
-       void setup_controllers(const boost::property_tree::wptree& pt)\r
-       {               \r
-               using boost::property_tree::wptree;\r
-               BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
-               {\r
-                       try\r
-                       {\r
-                               auto name = xml_controller.first;\r
-                               auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
-\r
-                               if(name == L"tcp")\r
-                               {                                       \r
-                                       unsigned int port = xml_controller.second.get(L"port", 5250);\r
-                                       auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(create_protocol(protocol), port);\r
-                                       async_servers_.push_back(asyncbootstrapper);\r
-                               }\r
-                               else\r
-                                       CASPAR_LOG(warning) << "Invalid controller: " << name;  \r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       }\r
-               }\r
-       }\r
-\r
-       spl::shared_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
-       {\r
-               if(boost::iequals(name, L"AMCP"))\r
-                       return spl::make_shared<amcp::AMCPProtocolStrategy>(channels_);\r
-               else if(boost::iequals(name, L"CII"))\r
-                       return spl::make_shared<cii::CIIProtocolStrategy>(channels_);\r
-               else if(boost::iequals(name, L"CLOCK"))\r
-                       return spl::make_shared<CLK::CLKProtocolStrategy>(channels_);\r
-               \r
-               CASPAR_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol"));\r
-       }\r
-};\r
-\r
-server::server() : impl_(new impl()){}\r
-\r
-const std::vector<spl::shared_ptr<video_channel>> server::channels() const\r
-{\r
-       return impl_->channels_;\r
-}\r
-void server::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}\r
-void server::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}\r
-\r
-}
\ No newline at end of file
+/*
+* 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 "included_modules.h"
+#include "default_audio_config.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/ptree.h>
+
+#include <core/video_channel.h>
+#include <core/video_format.h>
+#include <core/frame/audio_channel_layout.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/producer/color/color_producer.h>
+#include <core/consumer/output.h>
+#include <core/consumer/syncto/syncto_consumer.h>
+#include <core/mixer/mixer.h>
+#include <core/mixer/image/image_mixer.h>
+#include <core/thumbnail_generator.h>
+#include <core/producer/media_info/media_info.h>
+#include <core/producer/media_info/media_info_repository.h>
+#include <core/producer/media_info/in_memory_media_info_repository.h>
+#include <core/producer/cg_proxy.h>
+#include <core/diagnostics/subject_diagnostics.h>
+#include <core/diagnostics/call_context.h>
+#include <core/diagnostics/osd_graph.h>
+#include <core/diagnostics/graph_to_log_sink.h>
+#include <core/system_info_provider.h>
+#include <core/help/help_repository.h>
+
+#include <modules/image/consumer/image_consumer.h>
+
+#include <protocol/amcp/AMCPProtocolStrategy.h>
+#include <protocol/amcp/amcp_command_repository.h>
+#include <protocol/amcp/AMCPCommandsImpl.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 <protocol/log/tcp_logger_protocol_strategy.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 <boost/asio.hpp>
+
+#include <tbb/atomic.h>
+
+#include <future>
+
+namespace caspar {
+
+using namespace core;
+using namespace protocol;
+
+std::shared_ptr<boost::asio::io_service> create_running_io_service()
+{
+       auto service = std::make_shared<boost::asio::io_service>();
+       // To keep the io_service::run() running although no pending async
+       // operations are posted.
+       auto work = std::make_shared<boost::asio::io_service::work>(*service);
+       auto weak_work = std::weak_ptr<boost::asio::io_service::work>(work);
+       auto thread = std::make_shared<boost::thread>([service, weak_work]
+       {
+               ensure_gpf_handler_installed_for_thread("asio-thread");
+
+               while (auto strong = weak_work.lock())
+               {
+                       try
+                       {
+                               service->run();
+                       }
+                       catch (...)
+                       {
+                               CASPAR_LOG_CURRENT_EXCEPTION();
+                       }
+               }
+
+               CASPAR_LOG(info) << "[asio] Global io_service uninitialized.";
+       });
+
+       return std::shared_ptr<boost::asio::io_service>(
+                       service.get(),
+                       [service, work, thread](void*) mutable
+                       {
+                               CASPAR_LOG(info) << "[asio] Shutting down global io_service.";
+                               work.reset();
+                               service->stop();
+                               if (thread->get_id() != boost::this_thread::get_id())
+                                       thread->join();
+                               else
+                                       thread->detach();
+                       });
+}
+
+struct server::impl : boost::noncopyable
+{
+       std::shared_ptr<boost::asio::io_service>                        io_service_                                             = create_running_io_service();
+       spl::shared_ptr<monitor::subject>                                       monitor_subject_;
+       spl::shared_ptr<monitor::subject>                                       diag_subject_                                   = core::diagnostics::get_or_create_subject();
+       accelerator::accelerator                                                        accelerator_;
+       spl::shared_ptr<help_repository>                                        help_repo_;
+       std::shared_ptr<amcp::amcp_command_repository>          amcp_command_repo_;
+       std::vector<spl::shared_ptr<IO::AsyncEventServer>>      async_servers_;
+       std::shared_ptr<IO::AsyncEventServer>                           primary_amcp_server_;
+       std::shared_ptr<osc::client>                                            osc_client_                                             = std::make_shared<osc::client>(io_service_);
+       std::vector<std::shared_ptr<void>>                                      predefined_osc_subscriptions_;
+       std::vector<spl::shared_ptr<video_channel>>                     channels_;
+       spl::shared_ptr<media_info_repository>                          media_info_repo_;
+       boost::thread                                                                           initial_media_info_thread_;
+       spl::shared_ptr<system_info_provider_repository>        system_info_provider_repo_;
+       spl::shared_ptr<core::cg_producer_registry>                     cg_registry_;
+       spl::shared_ptr<core::frame_producer_registry>          producer_registry_;
+       spl::shared_ptr<core::frame_consumer_registry>          consumer_registry_;
+       tbb::atomic<bool>                                                                       running_;
+       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"))
+               , media_info_repo_(create_in_memory_media_info_repository())
+               , producer_registry_(spl::make_shared<core::frame_producer_registry>(help_repo_))
+               , consumer_registry_(spl::make_shared<core::frame_consumer_registry>(help_repo_))
+               , shutdown_server_now_(shutdown_server_now)
+       {
+               running_ = false;
+               core::diagnostics::register_graph_to_log_sink();
+               caspar::core::diagnostics::osd::register_sink();
+               diag_subject_->attach_parent(monitor_subject_);
+
+               module_dependencies dependencies(
+                               system_info_provider_repo_,
+                               cg_registry_,
+                               media_info_repo_,
+                               producer_registry_,
+                               consumer_registry_);
+
+               initialize_modules(dependencies);
+               core::text::init(dependencies);
+               core::scene::init(dependencies);
+               core::syncto::init(dependencies);
+               help_repo_->register_item({ L"producer" }, L"Color Producer", &core::describe_color_producer);
+       }
+
+       void start()
+       {
+               running_ = true;
+
+               setup_audio_config(env::properties());
+               CASPAR_LOG(info) << L"Initialized audio config.";
+
+               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.";
+
+               start_initial_media_info_scan();
+               CASPAR_LOG(info) << L"Started initial media information retrieval.";
+       }
+
+       ~impl()
+       {
+               if (running_)
+               {
+                       running_ = false;
+                       initial_media_info_thread_.join();
+               }
+
+               std::weak_ptr<boost::asio::io_service> weak_io_service = io_service_;
+               io_service_.reset();
+               osc_client_.reset();
+               thumbnail_generator_.reset();
+               amcp_command_repo_.reset();
+               primary_amcp_server_.reset();
+               async_servers_.clear();
+               destroy_producers_synchronously();
+               destroy_consumers_synchronously();
+               channels_.clear();
+
+               while (weak_io_service.lock())
+                       boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
+
+               uninitialize_modules();
+               core::diagnostics::osd::shutdown();
+       }
+
+       void setup_audio_config(const boost::property_tree::wptree& pt)
+       {
+               using boost::property_tree::wptree;
+
+               auto default_config = get_default_audio_config();
+
+               // Start with the defaults
+               audio_channel_layout_repository::get_default()->register_all_layouts(default_config.get_child(L"audio.channel-layouts"));
+               audio_mix_config_repository::get_default()->register_all_configs(default_config.get_child(L"audio.mix-configs"));
+
+               // Merge with user configuration (adds to or overwrites the defaults)
+               auto custom_channel_layouts     = pt.get_child_optional(L"configuration.audio.channel-layouts");
+               auto custom_mix_configs         = pt.get_child_optional(L"configuration.audio.mix-configs");
+
+               if (custom_channel_layouts)
+               {
+                       CASPAR_SCOPED_CONTEXT_MSG("/configuration/audio/channel-layouts");
+                       audio_channel_layout_repository::get_default()->register_all_layouts(*custom_channel_layouts);
+               }
+
+               if (custom_mix_configs)
+               {
+                       CASPAR_SCOPED_CONTEXT_MSG("/configuration/audio/mix-configs");
+                       audio_mix_config_repository::get_default()->register_all_configs(*custom_mix_configs);
+               }
+       }
+
+       void setup_channels(const boost::property_tree::wptree& pt)
+       {
+               using boost::property_tree::wptree;
+
+               std::vector<wptree> xml_channels;
+
+               for (auto& xml_channel : pt | witerate_children(L"configuration.channels") | welement_context_iteration)
+               {
+                       xml_channels.push_back(xml_channel.second);
+                       ptree_verify_element_name(xml_channel, L"channel");
+
+                       auto format_desc_str = xml_channel.second.get(L"video-mode", L"PAL");
+                       auto format_desc = video_format_desc(format_desc_str);
+                       if(format_desc.format == video_format::invalid)
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid video-mode: " + format_desc_str));
+
+                       auto channel_layout_str = xml_channel.second.get(L"channel-layout", L"stereo");
+                       auto channel_layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout_str);
+                       if (!channel_layout)
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Unknown channel-layout: " + channel_layout_str));
+
+                       auto channel_id = static_cast<int>(channels_.size() + 1);
+                       auto channel = spl::make_shared<video_channel>(channel_id, format_desc, *channel_layout, accelerator_.create_image_mixer(channel_id));
+
+                       channel->monitor_output().attach_parent(monitor_subject_);
+                       channel->mixer().set_straight_alpha_output(xml_channel.second.get(L"straight-alpha-output", false));
+                       channels_.push_back(channel);
+               }
+
+               for (auto& channel : channels_)
+               {
+                       core::diagnostics::scoped_call_context save;
+                       core::diagnostics::call_context::for_thread().video_channel = channel->index();
+
+                       for (auto& xml_consumer : xml_channels.at(channel->index() - 1) | witerate_children(L"consumers") | welement_context_iteration)
+                       {
+                               auto name = xml_consumer.first;
+
+                               try
+                               {
+                                       if (name != L"<xmlcomment>")
+                                               channel->output().add(consumer_registry_->create_consumer(name, xml_consumer.second, &channel->stage(), channels_));
+                               }
+                               catch (const user_error& e)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(debug);
+                                       CASPAR_LOG(error) << get_message_and_context(e) << " Turn on log level debug for stacktrace.";
+                               }
+                               catch (...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                               }
+                       }
+               }
+
+               // Dummy diagnostics channel
+               if (env::properties().get(L"configuration.channel-grid", false))
+               {
+                       auto channel_id = static_cast<int>(channels_.size() + 1);
+                       channels_.push_back(spl::make_shared<video_channel>(
+                                       channel_id,
+                                       core::video_format_desc(core::video_format::x576p2500),
+                                       *core::audio_channel_layout_repository::get_default()->get_layout(L"stereo"),
+                                       accelerator_.create_image_mixer(channel_id)));
+                       channels_.back()->monitor_output().attach_parent(monitor_subject_);
+               }
+       }
+
+       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 disable_send_to_amcp_clients =
+                               pt.get(L"configuration.osc.disable-send-to-amcp-clients", false);
+               auto predefined_clients =
+                               pt.get_child_optional(L"configuration.osc.predefined-clients");
+
+               if (predefined_clients)
+               {
+                       for (auto& predefined_client : pt | witerate_children(L"configuration.osc.predefined-clients") | welement_context_iteration)
+                       {
+                               ptree_verify_element_name(predefined_client, L"predefined-client");
+
+                               const auto address =
+                                               ptree_get<std::wstring>(predefined_client.second, L"address");
+                               const auto port =
+                                               ptree_get<unsigned short>(predefined_client.second, L"port");
+                               predefined_osc_subscriptions_.push_back(
+                                               osc_client_->get_subscription_token(udp::endpoint(
+                                                               address_v4::from_string(u8(address)),
+                                                               port)));
+                       }
+               }
+
+               if (!disable_send_to_amcp_clients && 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(io_service_, 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(0),
+                       pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),
+                       &image::write_cropped_png,
+                       media_info_repo_,
+                       producer_registry_,
+                       pt.get(L"configuration.thumbnails.mipmap", true)));
+       }
+
+       void setup_controllers(const boost::property_tree::wptree& pt)
+       {
+               amcp_command_repo_ = spl::make_shared<amcp::amcp_command_repository>(
+                               channels_,
+                               thumbnail_generator_,
+                               media_info_repo_,
+                               system_info_provider_repo_,
+                               cg_registry_,
+                               help_repo_,
+                               producer_registry_,
+                               consumer_registry_,
+                               accelerator_.get_ogl_device(),
+                               shutdown_server_now_);
+               amcp::register_commands(*amcp_command_repo_);
+
+               using boost::property_tree::wptree;
+               for (auto& xml_controller : pt | witerate_children(L"configuration.controllers") | welement_context_iteration)
+               {
+                       auto name = xml_controller.first;
+                       auto protocol = ptree_get<std::wstring>(xml_controller.second, L"protocol");
+
+                       if(name == L"tcp")
+                       {
+                               auto port = ptree_get<unsigned int>(xml_controller.second, L"port");
+                               auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(
+                                               io_service_,
+                                               create_protocol(protocol, L"TCP Port " + boost::lexical_cast<std::wstring>(port)),
+                                               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;
+               }
+       }
+
+       IO::protocol_strategy_factory<char>::ptr create_protocol(const std::wstring& name, const std::wstring& port_description) const
+       {
+               using namespace IO;
+
+               if(boost::iequals(name, L"AMCP"))
+                       return wrap_legacy_protocol("\r\n", spl::make_shared<amcp::AMCPProtocolStrategy>(port_description, spl::make_shared_ptr(amcp_command_repo_)));
+               else if(boost::iequals(name, L"CII"))
+                       return wrap_legacy_protocol("\r\n", spl::make_shared<cii::CIIProtocolStrategy>(channels_, cg_registry_, producer_registry_));
+               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_, cg_registry_, producer_registry_));
+               else if (boost::iequals(name, L"LOG"))
+                       return spl::make_shared<protocol::log::tcp_logger_protocol_strategy_factory>();
+
+               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid protocol: " + name));
+       }
+
+       void start_initial_media_info_scan()
+       {
+               initial_media_info_thread_ = boost::thread([this]
+               {
+                       try
+                       {
+                               ensure_gpf_handler_installed_for_thread("initial media scan");
+
+                               for (boost::filesystem::wrecursive_directory_iterator iter(env::media_folder()), end; iter != end; ++iter)
+                               {
+                                       if (running_)
+                                       {
+                                               if (boost::filesystem::is_regular_file(iter->path()))
+                                                       media_info_repo_->get(iter->path().wstring());
+                                       }
+                                       else
+                                       {
+                                               CASPAR_LOG(info) << L"Initial media information retrieval aborted.";
+                                               return;
+                                       }
+                               }
+
+                               CASPAR_LOG(info) << L"Initial media information retrieval finished.";
+                       }
+                       catch (...)
+                       {
+                               CASPAR_LOG_CURRENT_EXCEPTION();
+                       }
+               });
+       }
+};
+
+server::server(std::promise<bool>& shutdown_server_now) : impl_(new impl(shutdown_server_now)){}
+void server::start() { impl_->start(); }
+spl::shared_ptr<core::system_info_provider_repository> server::get_system_info_provider_repo() const { return impl_->system_info_provider_repo_; }
+spl::shared_ptr<protocol::amcp::amcp_command_repository> server::get_amcp_command_repository() const { return spl::make_shared_ptr(impl_->amcp_command_repo_); }
+core::monitor::subject& server::monitor_output() { return *impl_->monitor_subject_; }
+
+}