]> git.sesse.net Git - casparcg/blobdiff - core/producer/frame_producer.cpp
2.1.0: -transition_producer: Fixed problem with unpausing paused source clip.
[casparcg] / core / producer / frame_producer.cpp
index 2b0a295fba242cfec7c59ed3a4c094f84dddd654..f4984bc83031f0c5ba76c0803433b66543ff8dc7 100644 (file)
 \r
 #include <common/assert.h>\r
 #include <common/except.h>\r
-#include <common/concurrency/executor.h>\r
-#include <common/concurrency/async.h>\r
-#include <common/spl/memory.h>\r
+#include <common/executor.h>\r
+#include <common/future.h>\r
+#include <common/memory.h>\r
+\r
+#include <boost/thread.hpp>\r
 \r
 namespace caspar { namespace core {\r
        \r
@@ -44,80 +46,134 @@ void register_producer_factory(const producer_factory_t& factory)
        g_factories.push_back(factory);\r
 }\r
 \r
-spl::shared_ptr<class draw_frame> frame_producer::last_frame() const\r
+struct frame_producer_base::impl\r
+{\r
+       tbb::atomic<uint32_t>   frame_number_;\r
+       tbb::atomic<bool>               paused_;\r
+       frame_producer_base&    self_;\r
+       draw_frame                              last_frame_;\r
+\r
+       impl(frame_producer_base& self)\r
+               : self_(self)\r
+               , last_frame_(draw_frame::empty())\r
+       {\r
+               frame_number_ = 0;\r
+               paused_ = false;\r
+       }\r
+       \r
+       draw_frame receive()\r
+       {\r
+               if(paused_)\r
+                       return self_.last_frame();\r
+\r
+               auto frame = draw_frame::push(self_.receive_impl());\r
+               if(frame == draw_frame::late())\r
+                       return frame;\r
+\r
+               ++frame_number_;\r
+\r
+               return last_frame_ = frame;\r
+       }\r
+\r
+       void paused(bool value)\r
+       {\r
+               paused_ = value;\r
+       }\r
+\r
+       draw_frame last_frame() const\r
+       {\r
+               return draw_frame::still(last_frame_);\r
+       }\r
+};\r
+\r
+frame_producer_base::frame_producer_base() : impl_(new impl(*this))\r
 {\r
-       BOOST_THROW_EXCEPTION(not_implemented());\r
 }\r
 \r
-boost::unique_future<std::wstring> frame_producer::call(const std::wstring&) \r
+frame_producer_base::frame_producer_base(frame_producer_base& self) : impl_(new impl(self))\r
+{\r
+}\r
+\r
+draw_frame frame_producer_base::receive()\r
+{\r
+       return impl_->receive();\r
+}\r
+\r
+void frame_producer_base::paused(bool value)\r
+{\r
+       impl_->paused(value);\r
+}\r
+\r
+draw_frame frame_producer_base::last_frame() const\r
+{\r
+       return impl_->last_frame();\r
+}\r
+\r
+boost::unique_future<std::wstring> frame_producer_base::call(const std::wstring&) \r
 {\r
        BOOST_THROW_EXCEPTION(not_supported());\r
 }\r
 \r
-struct empty_frame_producer : public frame_producer\r
+uint32_t frame_producer_base::nb_frames() const\r
 {\r
-       virtual spl::shared_ptr<draw_frame> receive(int){return draw_frame::empty();}\r
-       virtual spl::shared_ptr<draw_frame> last_frame() const{return draw_frame::empty();}\r
-       virtual void set_frame_factory(const spl::shared_ptr<frame_factory>&){}\r
-       virtual uint32_t nb_frames() const {return 0;}\r
-       virtual std::wstring print() const { return L"empty";}\r
-       virtual void subscribe(const monitor::observable::observer_ptr& o){}\r
-       virtual void unsubscribe(const monitor::observable::observer_ptr& o){}  \r
-       virtual std::wstring name() const {return L"empty";}\r
-       \r
-       virtual boost::property_tree::wptree info() const override\r
-       {\r
-               boost::property_tree::wptree info;\r
-               info.add(L"type", L"empty-producer");\r
-               return info;\r
-       }\r
-};\r
+       return std::numeric_limits<uint32_t>::max();\r
+}\r
+\r
+uint32_t frame_producer_base::frame_number() const\r
+{\r
+       return impl_->frame_number_;\r
+}\r
 \r
-const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow\r
+const spl::shared_ptr<frame_producer>& frame_producer::empty() \r
 {\r
+       class empty_frame_producer : public frame_producer\r
+       {\r
+       public:\r
+               empty_frame_producer(){}\r
+               draw_frame receive() override{return draw_frame::empty();}\r
+               void paused(bool value) override{}\r
+               uint32_t nb_frames() const override {return 0;}\r
+               std::wstring print() const override { return L"empty";}\r
+               void subscribe(const monitor::observable::observer_ptr& o) override{}\r
+               void unsubscribe(const monitor::observable::observer_ptr& o) override{} \r
+               std::wstring name() const override {return L"empty";}\r
+               uint32_t frame_number() const override {return 0;}\r
+               boost::unique_future<std::wstring> call(const std::wstring& params) override{BOOST_THROW_EXCEPTION(not_supported());}\r
+               draw_frame last_frame() const {return draw_frame::empty();}\r
+       \r
+               boost::property_tree::wptree info() const override\r
+               {\r
+                       boost::property_tree::wptree info;\r
+                       info.add(L"type", L"empty-producer");\r
+                       return info;\r
+               }\r
+       };\r
+\r
        static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();\r
        return producer;\r
 }      \r
 \r
-class producer_proxy_base : public frame_producer\r
+class destroy_producer_proxy : public frame_producer\r
 {      \r
-protected:\r
        std::shared_ptr<frame_producer> producer_;\r
-public:\r
-       producer_proxy_base(spl::shared_ptr<frame_producer>&& producer) \r
-               : producer_(std::move(producer))\r
-       {\r
-       }\r
-       \r
-       virtual spl::shared_ptr<draw_frame>                                                     receive(int hints) override                                                                                                             {return producer_->receive(hints);}\r
-       virtual spl::shared_ptr<draw_frame>                                                     last_frame() const override                                                                                                             {return producer_->last_frame();}\r
-       virtual std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
-       virtual std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}\r
-       virtual boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
-       virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
-       virtual void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}\r
-       virtual uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
-       virtual void subscribe(const monitor::observable::observer_ptr& o)                                                                                                                                                      {return producer_->subscribe(o);}\r
-       virtual void unsubscribe(const monitor::observable::observer_ptr& o)                                                                                                                                            {return producer_->unsubscribe(o);}\r
-};\r
-\r
-class destroy_producer_proxy : public producer_proxy_base\r
-{      \r
 public:\r
        destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
-               : producer_proxy_base(std::move(producer))\r
+               : producer_(std::move(producer))\r
        {\r
        }\r
 \r
-       ~destroy_producer_proxy()\r
+       virtual ~destroy_producer_proxy()\r
        {               \r
                static tbb::atomic<int> counter = tbb::atomic<int>();\r
                \r
+               if(producer_ == core::frame_producer::empty())\r
+                       return;\r
+\r
                ++counter;\r
-               CASPAR_VERIFY(counter < 32);\r
+               CASPAR_VERIFY(counter < 8);\r
                \r
                auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));\r
-               async([=]\r
+               boost::thread([=]\r
                {\r
                        std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);\r
                        auto str = (*producer)->print();\r
@@ -129,61 +185,34 @@ public:
                                        CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
                        }\r
                        catch(...){}\r
-\r
+                       \r
                        pointer_guard.reset();\r
+                       CASPAR_LOG(info) << str << L" Destroyed.";\r
 \r
                        --counter;\r
-               }); \r
-       }\r
-};\r
-\r
-class print_producer_proxy : public producer_proxy_base\r
-{      \r
-public:\r
-       print_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
-               : producer_proxy_base(std::move(producer))\r
-       {\r
-               CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
-       }\r
-\r
-       ~print_producer_proxy()\r
-       {               \r
-               auto str = producer_->print();\r
-               CASPAR_LOG(trace) << str << L" Uninitializing.";\r
-               producer_.reset();\r
-               CASPAR_LOG(info) << str << L" Uninitialized.";\r
-       }\r
-};\r
-\r
-class last_frame_producer_proxy : public producer_proxy_base\r
-{      \r
-public:\r
-       last_frame_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
-               : producer_proxy_base(std::move(producer))\r
-               , last_frame_(draw_frame::empty())\r
-       {\r
-               CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
+               }).detach(); \r
        }\r
        \r
-       virtual spl::shared_ptr<draw_frame>     receive(int hints) override                                     \r
-       {\r
-               return last_frame_ = producer_->receive(hints);\r
-       }\r
-\r
-       virtual spl::shared_ptr<draw_frame>     last_frame() const override                                                                                                             \r
-       {\r
-               return last_frame_;\r
-       }\r
+       draw_frame      receive() override                                                                                                                                                                                                              {return producer_->receive();}\r
+       std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
+       void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}\r
+       std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}\r
+       uint32_t                                                                                        frame_number() const override                                                                                                   {return producer_->frame_number();}\r
+       boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
+       boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
+       void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}\r
+       uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
+       class draw_frame                                                                        last_frame() const                                                                                                                              {return producer_->last_frame();}\r
+       void                                                                                            subscribe(const monitor::observable::observer_ptr& o)                                                   {return producer_->subscribe(o);}\r
+       void                                                                                            unsubscribe(const monitor::observable::observer_ptr& o)                                                 {return producer_->unsubscribe(o);}\r
 };\r
 \r
-spl::shared_ptr<core::frame_producer> wrap_producer(spl::shared_ptr<core::frame_producer> producer)\r
+spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)\r
 {\r
-       return spl::make_shared<destroy_producer_proxy>(\r
-                        spl::make_shared<print_producer_proxy>(\r
-                         std::move(producer)));\r
+       return spl::make_shared<destroy_producer_proxy>(std::move(producer));\r
 }\r
 \r
-spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
+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)\r
 {\r
        if(params.empty())\r
                BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
@@ -193,7 +222,7 @@ spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<f
                {\r
                        try\r
                        {\r
-                               producer = factory(my_frame_factory, params);\r
+                               producer = factory(my_frame_factory, format_desc, params);\r
                        }\r
                        catch(...)\r
                        {\r
@@ -208,12 +237,12 @@ spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<f
        if(producer == frame_producer::empty())\r
                return producer;\r
                \r
-       return producer;\r
+       return create_destroy_proxy(producer);\r
 }\r
 \r
-spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
+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)\r
 {      \r
-       auto producer = do_create_producer(my_frame_factory, params);\r
+       auto producer = do_create_producer(my_frame_factory, format_desc, params);\r
        auto key_producer = frame_producer::empty();\r
        \r
        try // to find a key file.\r
@@ -222,11 +251,11 @@ spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<fram
                if(params_copy.size() > 0)\r
                {\r
                        params_copy[0] += L"_A";\r
-                       key_producer = do_create_producer(my_frame_factory, params_copy);                       \r
+                       key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);                  \r
                        if(key_producer == frame_producer::empty())\r
                        {\r
                                params_copy[0] += L"LPHA";\r
-                               key_producer = do_create_producer(my_frame_factory, params_copy);       \r
+                               key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);  \r
                        }\r
                }\r
        }\r
@@ -247,13 +276,13 @@ spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<fram
 }\r
 \r
 \r
-spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const std::wstring& params)\r
+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)\r
 {\r
        std::wstringstream iss(params);\r
        std::vector<std::wstring> tokens;\r
        typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;\r
        std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));\r
-       return create_producer(factory, tokens);\r
+       return create_producer(factory, format_desc, tokens);\r
 }\r
 \r
 }}
\ No newline at end of file