]> git.sesse.net Git - casparcg/blobdiff - core/producer/frame_producer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / frame_producer.cpp
index c966733cc4dec35af4d09df605ce883d8c16e7dc..2b78c61778998ba10c57bf77552893eb2a830224 100644 (file)
@@ -27,7 +27,6 @@
 #include "../frame/frame_transform.h"
 
 #include "color/color_producer.h"
-#include "draw/freehand_producer.h"
 #include "separated/separated_producer.h"
 #include "variable.h"
 
 #include <boost/thread.hpp>
 
 namespace caspar { namespace core {
-       
-std::vector<const producer_factory_t> g_factories;
-std::vector<const producer_factory_t> g_thumbnail_factories;
 
-void register_producer_factory(const producer_factory_t& factory)
+struct frame_producer_registry::impl
+{
+       std::vector<producer_factory_t>         producer_factories;
+       std::vector<thumbnail_producer_t>       thumbnail_producers;
+       spl::shared_ptr<help_repository>        help_repo;
+
+       impl(spl::shared_ptr<help_repository> help_repo)
+               : help_repo(std::move(help_repo))
+       {
+       }
+};
+
+frame_producer_registry::frame_producer_registry(spl::shared_ptr<help_repository> help_repo)
+    : impl_(new impl(std::move(help_repo)))
+{
+}
+
+void frame_producer_registry::register_producer_factory(std::wstring name, const producer_factory_t& factory, const help_item_describer& describer)
 {
-       g_factories.push_back(factory);
+       impl_->producer_factories.push_back(factory);
+       impl_->help_repo->register_item({ L"producer" }, std::move(name), describer);
 }
-void register_thumbnail_producer_factory(const producer_factory_t& factory)
+
+void frame_producer_registry::register_thumbnail_producer(const thumbnail_producer_t& thumbnail_producer)
+{
+       impl_->thumbnail_producers.push_back(thumbnail_producer);
+}
+
+frame_producer_dependencies::frame_producer_dependencies(
+               const spl::shared_ptr<core::frame_factory>& frame_factory,
+               const std::vector<spl::shared_ptr<video_channel>>& channels,
+               const video_format_desc& format_desc,
+               const spl::shared_ptr<const frame_producer_registry> producer_registry)
+       : frame_factory(frame_factory)
+       , channels(channels)
+       , format_desc(format_desc)
+       , producer_registry(producer_registry)
 {
-       g_thumbnail_factories.push_back(factory);
 }
 
 constraints::constraints(double width, double height)
@@ -100,10 +127,6 @@ struct frame_producer_base::impl
        {
                return draw_frame::still(last_frame_);
        }
-       draw_frame create_thumbnail_frame()
-       {
-               return draw_frame::empty();
-       }
 };
 
 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
@@ -124,10 +147,6 @@ draw_frame frame_producer_base::last_frame()
 {
        return impl_->last_frame();
 }
-draw_frame frame_producer_base::create_thumbnail_frame()
-{
-       return impl_->create_thumbnail_frame();
-}
 
 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&) 
 {
@@ -146,7 +165,7 @@ uint32_t frame_producer_base::frame_number() const
 
 variable& frame_producer_base::get_variable(const std::wstring& name)
 {
-       CASPAR_THROW_EXCEPTION(caspar_exception() 
+       CASPAR_THROW_EXCEPTION(user_error()
                        << msg_info(L"No variable called " + name + L" found in " + print()));
 }
 
@@ -170,11 +189,10 @@ const spl::shared_ptr<frame_producer>& frame_producer::empty()
                monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}                                                                              
                std::wstring name() const override {return L"empty";}
                uint32_t frame_number() const override {return 0;}
-               std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_supported());}
-               variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_supported()); }
+               std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_implemented());}
+               variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_implemented()); }
                const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
                draw_frame last_frame() {return draw_frame::empty();}
-               draw_frame create_thumbnail_frame() {return draw_frame::empty();}
                constraints& pixel_constraints() override { static constraints c; return c; }
        
                boost::property_tree::wptree info() const override
@@ -187,7 +205,19 @@ const spl::shared_ptr<frame_producer>& frame_producer::empty()
 
        static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
        return producer;
-}      
+}
+
+tbb::atomic<bool>& destroy_producers_in_separate_thread()
+{
+       static tbb::atomic<bool> state;
+
+       return state;
+}
+
+void destroy_producers_synchronously()
+{
+       destroy_producers_in_separate_thread() = false;
+}
 
 class destroy_producer_proxy : public frame_producer
 {      
@@ -196,13 +226,16 @@ public:
        destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) 
                : producer_(std::move(producer))
        {
+               destroy_producers_in_separate_thread() = true;
        }
 
        virtual ~destroy_producer_proxy()
-       {               
-               static tbb::atomic<int> counter = tbb::atomic<int>();
+       {
+               static tbb::atomic<int> counter;
+               static std::once_flag counter_init_once;
+               std::call_once(counter_init_once, []{ counter = 0; });
                
-               if(producer_ == core::frame_producer::empty())
+               if(producer_ == core::frame_producer::empty() || !destroy_producers_in_separate_thread())
                        return;
 
                ++counter;
@@ -215,10 +248,12 @@ public:
                        auto str = (*producer)->print();
                        try
                        {
-                               if(!producer->unique())
-                                       CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
+                               ensure_gpf_handler_installed_for_thread(u8(L"Destroyer: " + str).c_str());
+
+                               if (!producer->unique())
+                                       CASPAR_LOG(debug) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
                                else
-                                       CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";
+                                       CASPAR_LOG(debug) << str << L" Destroying on asynchronous destruction thread.";
                        }
                        catch(...){}
                        
@@ -236,7 +271,7 @@ public:
                }).detach(); 
        }
        
-       draw_frame      receive() override                                                                                                                                                                                                              {return producer_->receive();}
+       draw_frame                                                                                      receive() override                                                                                                                                                                                                              {return producer_->receive();}
        std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}
        void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}
        std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}
@@ -247,11 +282,10 @@ public:
        const std::vector<std::wstring>&                                        get_variables() const override                                                                                                  {return producer_->get_variables();}
        void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}
        uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}
-       class draw_frame                                                                        last_frame()                                                                                                                                    {return producer_->last_frame();}
-       draw_frame                                                                                      create_thumbnail_frame()                                                                                                                {return producer_->create_thumbnail_frame();}
+       draw_frame                                                                                      last_frame()                                                                                                                                    {return producer_->last_frame();}
        monitor::subject&                                                                       monitor_output() override                                                                                                               {return producer_->monitor_output();}                                                                           
-       bool                                                                                            collides(double x, double y)                                                                                                    {return producer_->collides(x, y);}
-       void                                                                                            on_interaction(const interaction_event::ptr& event)                                                             {return producer_->on_interaction(event);}
+       bool                                                                                            collides(double x, double y) const override                                                                             {return producer_->collides(x, y);}
+       void                                                                                            on_interaction(const interaction_event::ptr& event)     override                                        {return producer_->on_interaction(event);}
        constraints&                                                                            pixel_constraints() override                                                                                                    {return producer_->pixel_constraints();}
 };
 
@@ -260,17 +294,21 @@ spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core:
        return spl::make_shared<destroy_producer_proxy>(std::move(producer));
 }
 
-spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params, const std::vector<const producer_factory_t>& factories, bool throw_on_fail = false)
+spl::shared_ptr<core::frame_producer> do_create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params, const std::vector<producer_factory_t>& factories, bool throw_on_fail = false)
 {
        if(params.empty())
-               CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));
+               CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("params cannot be empty"));
        
        auto producer = frame_producer::empty();
        std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
                {
                        try
                        {
-                               producer = factory(my_frame_factory, format_desc, params);
+                               producer = factory(dependencies, params);
+                       }
+                       catch (user_error&)
+                       {
+                               throw;
                        }
                        catch(...)
                        {
@@ -283,10 +321,7 @@ spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<f
                });
 
        if(producer == frame_producer::empty())
-               producer = create_color_producer(my_frame_factory, params);
-
-       if (producer == frame_producer::empty())
-               producer = create_freehand_producer(my_frame_factory, params);
+               producer = create_color_producer(dependencies.frame_factory, params);
 
        if(producer == frame_producer::empty())
                return producer;
@@ -294,39 +329,44 @@ spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<f
        return producer;
 }
 
-spl::shared_ptr<core::frame_producer> create_thumbnail_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::wstring& media_file)
+draw_frame do_create_thumbnail_frame(
+               const frame_producer_dependencies& dependencies,
+               const std::wstring& media_file,
+               const std::vector<thumbnail_producer_t>& thumbnail_producers)
 {
-  std::vector<std::wstring> params;
-  params.push_back(media_file);
-
-  auto producer = do_create_producer(my_frame_factory, format_desc, params, g_thumbnail_factories, true);
-  auto key_producer = frame_producer::empty();
-  
-  try // to find a key file.
-  {
-       auto params_copy = params;
-       if (params_copy.size() > 0)
+       for (auto& thumbnail_producer : thumbnail_producers)
        {
-         params_copy[0] += L"_A";
-         key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_thumbnail_factories, true);
-         if (key_producer == frame_producer::empty())
-         {
-               params_copy[0] += L"LPHA";
-               key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_thumbnail_factories, true);
-         }
+               auto frame = thumbnail_producer(dependencies, media_file);
+
+               if (frame != draw_frame::empty())
+                       return frame;
        }
-  }
-  catch(...){}
 
-  if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
-       return create_separated_producer(producer, key_producer);
+       return draw_frame::empty();
+}
+
+draw_frame frame_producer_registry::create_thumbnail(const frame_producer_dependencies& dependencies, const std::wstring& media_file) const
+{
+       auto& thumbnail_producers = impl_->thumbnail_producers;
+       std::vector<std::wstring> params;
+       params.push_back(media_file);
+
+       auto fill_frame = do_create_thumbnail_frame(dependencies, media_file, thumbnail_producers);
+       auto key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_A", thumbnail_producers);
+
+       if (key_frame == draw_frame::empty())
+               key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_ALPHA", thumbnail_producers);
+  
+       if (fill_frame != draw_frame::empty() && key_frame != draw_frame::empty())
+               return draw_frame::mask(fill_frame, key_frame);
   
-  return producer;
+       return fill_frame;
 }
 
-spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const video_format_desc& format_desc, const std::vector<std::wstring>& params)
+spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params) const
 {      
-       auto producer = do_create_producer(my_frame_factory, format_desc, params, g_factories);
+       auto& producer_factories = impl_->producer_factories;
+       auto producer = do_create_producer(dependencies, params, producer_factories);
        auto key_producer = frame_producer::empty();
        
        try // to find a key file.
@@ -335,11 +375,11 @@ spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<fram
                if(params_copy.size() > 0)
                {
                        params_copy[0] += L"_A";
-                       key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_factories);                     
+                       key_producer = do_create_producer(dependencies, params_copy, producer_factories);
                        if(key_producer == frame_producer::empty())
                        {
                                params_copy[0] += L"LPHA";
-                               key_producer = do_create_producer(my_frame_factory, format_desc, params_copy, g_factories);     
+                               key_producer = do_create_producer(dependencies, params_copy, producer_factories);
                        }
                }
        }
@@ -351,7 +391,7 @@ spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<fram
        if(producer == frame_producer::empty())
        {
                std::wstring str;
-               BOOST_FOREACH(auto& param, params)
+               for (auto& param : params)
                        str += param + L" ";
                CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
        }
@@ -360,13 +400,13 @@ spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<fram
 }
 
 
-spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const video_format_desc& format_desc, const std::wstring& params)
+spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params) const
 {
        std::wstringstream iss(params);
        std::vector<std::wstring> tokens;
        typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
        std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));
-       return create_producer(factory, format_desc, tokens);
+       return create_producer(dependencies, tokens);
 }
 
-}}
\ No newline at end of file
+}}