]> git.sesse.net Git - casparcg/blobdiff - shell/server.cpp
- Enabled a single instance of decklink_consumer to manage a separate key output...
[casparcg] / shell / server.cpp
index 08896e277e1b9c67e052600bcf4dea6c3908077a..86b1736157d75f0902e11f410d723caec961e5a4 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
 * This file is part of CasparCG (www.casparcg.com).\r
 *\r
 #include <common/filesystem/polling_filesystem_monitor.h>\r
 \r
 #include <core/mixer/gpu/ogl_device.h>\r
+#include <core/mixer/audio/audio_util.h>\r
+#include <core/mixer/mixer.h>\r
 #include <core/video_channel.h>\r
 #include <core/producer/stage.h>\r
 #include <core/consumer/output.h>\r
 #include <core/thumbnail_generator.h>\r
+#include <core/producer/media_info/media_info.h>\r
+#include <core/producer/media_info/media_info_repository.h>\r
+#include <core/producer/media_info/in_memory_media_info_repository.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/html/html.h>\r
 #include <modules/oal/oal.h>\r
 #include <modules/ogl/ogl.h>\r
-#include <modules/silverlight/silverlight.h>\r
+#include <modules/newtek/newtek.h>\r
 #include <modules/image/image.h>\r
 #include <modules/image/consumer/image_consumer.h>\r
 \r
 #include <modules/oal/consumer/oal_consumer.h>\r
 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
+#include <modules/newtek/consumer/newtek_ivga_consumer.h>\r
 #include <modules/decklink/consumer/decklink_consumer.h>\r
+#include <modules/decklink/consumer/blocking_decklink_consumer.h>\r
 #include <modules/ogl/consumer/ogl_consumer.h>\r
 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
 \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
+#include <boost/asio.hpp>\r
+\r
+#include <tbb/atomic.h>\r
 \r
 namespace caspar {\r
 \r
 using namespace core;\r
 using namespace protocol;\r
 \r
+std::shared_ptr<boost::asio::io_service> create_running_io_service()\r
+{\r
+       auto service = std::make_shared<boost::asio::io_service>();\r
+       // To keep the io_service::run() running although no pending async\r
+       // operations are posted.\r
+       auto work = std::make_shared<boost::asio::io_service::work>(*service);\r
+       auto thread = std::make_shared<boost::thread>([service]\r
+       {\r
+               win32_exception::ensure_handler_installed_for_thread("asio-thread");\r
+\r
+               service->run();\r
+       });\r
+\r
+       return std::shared_ptr<boost::asio::io_service>(\r
+                       service.get(),\r
+                       [service, work, thread] (void*) mutable\r
+                       {\r
+                               work.reset();\r
+                               service->stop();\r
+                               thread->join();\r
+                       });\r
+}\r
+\r
 struct server::implementation : boost::noncopyable\r
 {\r
-       core::monitor::subject                                          monitor_subject_;\r
+       std::shared_ptr<boost::asio::io_service>        io_service_;\r
+       safe_ptr<core::monitor::subject>                        monitor_subject_;\r
        boost::promise<bool>&                                           shutdown_server_now_;\r
        safe_ptr<ogl_device>                                            ogl_;\r
        std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;     \r
-       std::vector<osc::client>                                        osc_client_;\r
+       std::shared_ptr<IO::AsyncEventServer>           primary_amcp_server_;\r
+       osc::client                                                                     osc_client_;\r
+       std::vector<std::shared_ptr<void>>                      predefined_osc_subscriptions_;\r
        std::vector<safe_ptr<video_channel>>            channels_;\r
+       safe_ptr<media_info_repository>                         media_info_repo_;\r
+       boost::thread                                                           initial_media_info_thread_;\r
+       tbb::atomic<bool>                                                       running_;\r
        std::shared_ptr<thumbnail_generator>            thumbnail_generator_;\r
 \r
        implementation(boost::promise<bool>& shutdown_server_now)\r
-               : shutdown_server_now_(shutdown_server_now)\r
+               : io_service_(create_running_io_service())\r
+               , shutdown_server_now_(shutdown_server_now)\r
                , ogl_(ogl_device::create())\r
-       {                       \r
-               ffmpeg::init();\r
+               , osc_client_(io_service_)\r
+               , media_info_repo_(create_in_memory_media_info_repository())\r
+       {\r
+               running_ = true;\r
+               setup_audio(env::properties());\r
+               \r
+               html::init();\r
+               CASPAR_LOG(info) << L"Initialized html module.";\r
+\r
+               ffmpeg::init(media_info_repo_);\r
                CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
                                                          \r
                bluefish::init();         \r
@@ -91,19 +141,22 @@ struct server::implementation : boost::noncopyable
                                                          \r
                decklink::init();         \r
                CASPAR_LOG(info) << L"Initialized decklink module.";\r
-                                                                                                                 \r
-               oal::init();              \r
+\r
+               oal::init();\r
                CASPAR_LOG(info) << L"Initialized oal module.";\r
                                                          \r
+               newtek::init();\r
+               CASPAR_LOG(info) << L"Initialized newtek module.";\r
+\r
                ogl::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
+               image::init();            \r
+               CASPAR_LOG(info) << L"Initialized image module.";\r
+\r
                setup_channels(env::properties());\r
                CASPAR_LOG(info) << L"Initialized channels.";\r
 \r
@@ -111,14 +164,45 @@ struct server::implementation : boost::noncopyable
 \r
                setup_controllers(env::properties());\r
                CASPAR_LOG(info) << L"Initialized controllers.";\r
+\r
+               setup_osc(env::properties());\r
+               CASPAR_LOG(info) << L"Initialized osc.";\r
+\r
+               start_initial_media_info_scan();\r
+               CASPAR_LOG(info) << L"Started initial media information retrieval.";\r
        }\r
 \r
        ~implementation()\r
-       {               \r
-               ffmpeg::uninit();\r
-\r
+       {\r
+               running_ = false;\r
+               initial_media_info_thread_.join();\r
+               thumbnail_generator_.reset();\r
+               primary_amcp_server_.reset();\r
                async_servers_.clear();\r
+               destroy_producers_synchronously();\r
                channels_.clear();\r
+\r
+               html::uninit();\r
+               ffmpeg::uninit();\r
+       }\r
+\r
+       void setup_audio(const boost::property_tree::wptree& pt)\r
+       {\r
+               register_default_channel_layouts(default_channel_layout_repository());\r
+               register_default_mix_configs(default_mix_config_repository());\r
+\r
+               auto channel_layouts =\r
+                       pt.get_child_optional(L"configuration.audio.channel-layouts");\r
+               auto mix_configs =\r
+                       pt.get_child_optional(L"configuration.audio.mix-configs");\r
+\r
+               if (channel_layouts)\r
+                       parse_channel_layouts(\r
+                                       default_channel_layout_repository(), *channel_layouts);\r
+\r
+               if (mix_configs)\r
+                       parse_mix_configs(\r
+                                       default_mix_config_repository(), *mix_configs);\r
        }\r
                                \r
        void setup_channels(const boost::property_tree::wptree& pt)\r
@@ -129,39 +213,75 @@ struct server::implementation : boost::noncopyable
                        auto format_desc = video_format_desc::get(widen(xml_channel.second.get(L"video-mode", L"PAL")));                \r
                        if(format_desc.format == video_format::invalid)\r
                                BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
+                       auto audio_channel_layout = default_channel_layout_repository().get_by_name(\r
+                                       boost::to_upper_copy(xml_channel.second.get(L"channel-layout", L"STEREO")));\r
                        \r
-                       channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_));\r
+                       channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_, audio_channel_layout));\r
                        \r
-                       channels_.back()->monitor_output().link_target(&monitor_subject_);\r
+                       channels_.back()->monitor_output().attach_parent(monitor_subject_);\r
+                       channels_.back()->mixer()->set_straight_alpha_output(\r
+                                       xml_channel.second.get(L"straight-alpha-output", false));\r
 \r
-                       BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))\r
-                       {\r
-                               try\r
+                       create_consumers(\r
+                               xml_channel.second.get_child(L"consumers"),\r
+                               [&] (const safe_ptr<core::frame_consumer>& consumer)\r
                                {\r
-                                       auto name = xml_consumer.first;\r
-                                       if(name == L"screen")\r
-                                               channels_.back()->output()->add(ogl::create_consumer(xml_consumer.second));                                     \r
-                                       else if(name == L"bluefish")                                    \r
-                                               channels_.back()->output()->add(bluefish::create_consumer(xml_consumer.second));                                        \r
-                                       else if(name == L"decklink")                                    \r
-                                               channels_.back()->output()->add(decklink::create_consumer(xml_consumer.second));                                \r
-                                       else if(name == L"file")                                        \r
-                                               channels_.back()->output()->add(ffmpeg::create_consumer(xml_consumer.second));                                          \r
-                                       else if(name == L"system-audio")\r
-                                               channels_.back()->output()->add(oal::create_consumer());                \r
-                                       else if(name != L"<xmlcomment>")\r
-                                               CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
-                               }\r
-                               catch(...)\r
-                               {\r
-                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               }\r
-                       }                                                       \r
+                                       channels_.back()->output()->add(consumer);\r
+                               });\r
                }\r
 \r
                // Dummy diagnostics channel\r
                if(env::properties().get(L"configuration.channel-grid", false))\r
-                       channels_.push_back(make_safe<video_channel>(channels_.size()+1, core::video_format_desc::get(core::video_format::x576p2500), ogl_));\r
+               {\r
+                       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
+                       channels_.back()->monitor_output().attach_parent(monitor_subject_);\r
+               }\r
+       }\r
+\r
+       template<typename Base>\r
+       std::vector<safe_ptr<Base>> create_consumers(const boost::property_tree::wptree& pt)\r
+       {\r
+               std::vector<safe_ptr<Base>> consumers;\r
+\r
+               create_consumers(pt, [&] (const safe_ptr<core::frame_consumer>& consumer)\r
+               {\r
+                       consumers.push_back(dynamic_pointer_cast<Base>(consumer));\r
+               });\r
+\r
+               return consumers;\r
+       }\r
+\r
+       template<class Func>\r
+       void create_consumers(const boost::property_tree::wptree& pt, const Func& on_consumer)\r
+       {\r
+               BOOST_FOREACH(auto& xml_consumer, pt)\r
+               {\r
+                       try\r
+                       {\r
+                               auto name = xml_consumer.first;\r
+\r
+                               if (name == L"screen")\r
+                                       on_consumer(ogl::create_consumer(xml_consumer.second));\r
+                               else if (name == L"bluefish")                                   \r
+                                       on_consumer(bluefish::create_consumer(xml_consumer.second));                                    \r
+                               else if (name == L"decklink")                                   \r
+                                       on_consumer(decklink::create_consumer(xml_consumer.second));                            \r
+                               else if (name == L"newtek-ivga")                                        \r
+                                       on_consumer(newtek::create_ivga_consumer(xml_consumer.second));                 \r
+                               else if (name == L"blocking-decklink")\r
+                                       on_consumer(decklink::create_blocking_consumer(xml_consumer.second));                           \r
+                               else if (name == L"file" || name == L"stream")                                  \r
+                                       on_consumer(ffmpeg::create_consumer(xml_consumer.second));                                              \r
+                               else if (name == L"system-audio")\r
+                                       on_consumer(oal::create_consumer());\r
+                               else if (name != L"<xmlcomment>")\r
+                                       CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
+                       }\r
+                       catch(...)\r
+                       {\r
+                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       }\r
+               }\r
        }\r
                \r
        void setup_controllers(const boost::property_tree::wptree& pt)\r
@@ -180,13 +300,9 @@ struct server::implementation : boost::noncopyable
                                        auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
                                        asyncbootstrapper->Start();\r
                                        async_servers_.push_back(asyncbootstrapper);\r
-                               }\r
-                               else if(name == L"udp")\r
-                               {                                       \r
-                                       const auto address = xml_controller.second.get(L"address", L"127.0.0.1");\r
-                                       const auto port = xml_controller.second.get<unsigned short>(L"port", 5253);\r
 \r
-                                       osc_client_.push_back(osc::client(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::from_string(narrow(address)), port), monitor_subject_));\r
+                                       if (!primary_amcp_server_ && boost::iequals(protocol, L"AMCP"))\r
+                                               primary_amcp_server_ = asyncbootstrapper;\r
                                }\r
                                else\r
                                        CASPAR_LOG(warning) << "Invalid controller: " << widen(name);   \r
@@ -198,6 +314,47 @@ struct server::implementation : boost::noncopyable
                }\r
        }\r
 \r
+       void setup_osc(const boost::property_tree::wptree& pt)\r
+       {               \r
+               using boost::property_tree::wptree;\r
+               using namespace boost::asio::ip;\r
+\r
+               monitor_subject_->attach_parent(osc_client_.sink());\r
+               \r
+               auto default_port =\r
+                               pt.get<unsigned short>(L"configuration.osc.default-port", 6250);\r
+               auto predefined_clients =\r
+                               pt.get_child_optional(L"configuration.osc.predefined-clients");\r
+\r
+               if (predefined_clients)\r
+               {\r
+                       BOOST_FOREACH(auto& predefined_client, *predefined_clients)\r
+                       {\r
+                               const auto address =\r
+                                               predefined_client.second.get<std::wstring>(L"address");\r
+                               const auto port =\r
+                                               predefined_client.second.get<unsigned short>(L"port");\r
+                               predefined_osc_subscriptions_.push_back(\r
+                                               osc_client_.get_subscription_token(udp::endpoint(\r
+                                                               address_v4::from_string(narrow(address)),\r
+                                                               port)));\r
+                       }\r
+               }\r
+\r
+               if (primary_amcp_server_)\r
+                       primary_amcp_server_->add_lifecycle_factory(\r
+                                       [=] (const std::string& ipv4_address)\r
+                                                       -> std::shared_ptr<void>\r
+                                       {\r
+                                               using namespace boost::asio::ip;\r
+\r
+                                               return osc_client_.get_subscription_token(\r
+                                                               udp::endpoint(\r
+                                                                               address_v4::from_string(ipv4_address),\r
+                                                                               default_port));\r
+                                       });\r
+       }\r
+\r
        void setup_thumbnail_generation(const boost::property_tree::wptree& pt)\r
        {\r
                if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))\r
@@ -205,7 +362,8 @@ struct server::implementation : boost::noncopyable
 \r
                auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);\r
 \r
-               polling_filesystem_monitor_factory monitor_factory(scan_interval_millis);\r
+               polling_filesystem_monitor_factory monitor_factory(\r
+                               io_service_, scan_interval_millis);\r
                thumbnail_generator_.reset(new thumbnail_generator(\r
                                monitor_factory, \r
                                env::media_folder(),\r
@@ -215,7 +373,8 @@ struct server::implementation : boost::noncopyable
                                core::video_format_desc::get(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),\r
                                ogl_,\r
                                pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),\r
-                               &image::write_cropped_png));\r
+                               &image::write_cropped_png,\r
+                               media_info_repo_));\r
 \r
                CASPAR_LOG(info) << L"Initialized thumbnail generator.";\r
        }\r
@@ -223,7 +382,7 @@ struct server::implementation : boost::noncopyable
        safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
        {\r
                if(boost::iequals(name, L"AMCP"))\r
-                       return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, shutdown_server_now_);\r
+                       return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_, media_info_repo_, shutdown_server_now_);\r
                else if(boost::iequals(name, L"CII"))\r
                        return make_safe<cii::CIIProtocolStrategy>(channels_);\r
                else if(boost::iequals(name, L"CLOCK"))\r
@@ -235,6 +394,25 @@ struct server::implementation : boost::noncopyable
                \r
                BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(narrow(name)) << msg_info("Invalid protocol"));\r
        }\r
+\r
+       void start_initial_media_info_scan()\r
+       {\r
+               initial_media_info_thread_ = boost::thread([this]\r
+               {\r
+                       for (boost::filesystem::wrecursive_directory_iterator iter(env::media_folder()), end; iter != end; ++iter)\r
+                       {\r
+                               if (running_)\r
+                                       media_info_repo_->get(iter->path().file_string());\r
+                               else\r
+                               {\r
+                                       CASPAR_LOG(info) << L"Initial media information retrieval aborted.";\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
+                       CASPAR_LOG(info) << L"Initial media information retrieval finished.";\r
+               });\r
+       }\r
 };\r
 \r
 server::server(boost::promise<bool>& shutdown_server_now) : impl_(new implementation(shutdown_server_now)){}\r
@@ -249,9 +427,14 @@ std::shared_ptr<thumbnail_generator> server::get_thumbnail_generator() const
        return impl_->thumbnail_generator_;\r
 }\r
 \r
-core::monitor::source& server::monitor_output()\r
+safe_ptr<media_info_repository> server::get_media_info_repo() const\r
+{\r
+       return impl_->media_info_repo_;\r
+}\r
+\r
+core::monitor::subject& server::monitor_output()\r
 {\r
-       return impl_->monitor_subject_;\r
+       return *impl_->monitor_subject_;\r
 }\r
 \r
 }
\ No newline at end of file