]> git.sesse.net Git - casparcg/blobdiff - core/producer/frame_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / frame_producer.cpp
index 9b187d834b6861be397d4b58d9e1c0c642135433..4379100ed0faa6794905a1dfda68257b71880850 100644 (file)
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\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
+* 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
+* 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
 \r
 #include "../StdAfx.h"\r
 \r
 #include "frame_producer.h"\r
-#include "frame/basic_frame.h"\r
-#include "frame/audio_transform.h"\r
+\r
+#include "../frame/draw_frame.h"\r
+#include "../frame/frame_transform.h"\r
 \r
 #include "color/color_producer.h"\r
 #include "separated/separated_producer.h"\r
 \r
-#include <common/memory/safe_ptr.h>\r
-#include <common/exception/exceptions.h>\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
 \r
 namespace caspar { namespace core {\r
        \r
 std::vector<const producer_factory_t> g_factories;\r
 \r
-const safe_ptr<frame_producer>& frame_producer::empty() // nothrow\r
+void register_producer_factory(const producer_factory_t& factory)\r
 {\r
-       struct empty_frame_producer : public frame_producer\r
+       g_factories.push_back(factory);\r
+}\r
+\r
+boost::unique_future<std::wstring> frame_producer::call(const std::wstring&) \r
+{\r
+       BOOST_THROW_EXCEPTION(not_supported());\r
+}\r
+\r
+const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow\r
+{\r
+\r
+struct empty_frame_producer : public frame_producer\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
-               virtual safe_ptr<basic_frame> receive(int){return basic_frame::empty();}\r
-               virtual safe_ptr<basic_frame> last_frame() const{return basic_frame::empty();}\r
-               virtual void set_frame_factory(const safe_ptr<frame_factory>&){}\r
-               virtual int64_t nb_frames() const {return 0;}\r
-               virtual std::wstring print() const { return L"empty";}\r
-       };\r
-       static safe_ptr<frame_producer> producer = make_safe<empty_frame_producer>();\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
-safe_ptr<basic_frame> receive_and_follow(safe_ptr<frame_producer>& producer, int hints)\r
+class producer_proxy : public frame_producer\r
 {      \r
-       auto frame = producer->receive(hints);\r
-       if(frame == basic_frame::eof())\r
+       std::shared_ptr<frame_producer> producer_;\r
+public:\r
+       producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
+               : producer_(std::move(producer))\r
        {\r
-               CASPAR_LOG(info) << producer->print() << " End Of File.";\r
-               auto following = producer->get_following_producer();\r
-               following->set_leading_producer(producer);\r
-               producer = std::move(following);                \r
-                               \r
-               if(producer == frame_producer::empty())\r
-                       return basic_frame::eof();\r
-\r
-               return receive_and_follow(producer, hints);\r
+               CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
        }\r
-       return frame;\r
-}\r
 \r
-void register_producer_factory(const producer_factory_t& factory)\r
+       virtual ~producer_proxy()\r
+       {               \r
+               static tbb::atomic<int> counter = tbb::atomic<int>();\r
+               \r
+               ++counter;\r
+               CASPAR_VERIFY(counter < 32);\r
+               \r
+               auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));\r
+               async([=]\r
+               {\r
+                       std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);\r
+                       auto str = (*producer)->print();\r
+                       try\r
+                       {\r
+                               if(!producer->unique())\r
+                                       CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();\r
+                               else\r
+                                       CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
+                       }\r
+                       catch(...){}\r
+                       \r
+                       CASPAR_LOG(trace) << str << L" Uninitializing.";\r
+                       pointer_guard.reset();\r
+                       CASPAR_LOG(info) << str << L" Uninitialized.";\r
+\r
+                       --counter;\r
+               }); \r
+       }\r
+       \r
+       virtual spl::shared_ptr<draw_frame>     receive(int flags) override                                                                                                                                                             {return producer_->receive(flags);}\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
+spl::shared_ptr<core::frame_producer> wrap_producer(spl::shared_ptr<core::frame_producer> producer)\r
 {\r
-       g_factories.push_back(factory);\r
+       return spl::make_shared<producer_proxy>(std::move(producer));\r
 }\r
 \r
-safe_ptr<core::frame_producer> do_create_producer(const safe_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 std::vector<std::wstring>& params)\r
 {\r
        if(params.empty())\r
                BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
@@ -92,12 +152,14 @@ safe_ptr<core::frame_producer> do_create_producer(const safe_ptr<frame_factory>&
 \r
        if(producer == frame_producer::empty())\r
                producer = create_color_producer(my_frame_factory, params);\r
-       \r
+\r
+       if(producer == frame_producer::empty())\r
+               return producer;\r
+               \r
        return producer;\r
 }\r
 \r
-\r
-safe_ptr<core::frame_producer> create_producer(const safe_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 std::vector<std::wstring>& params)\r
 {      \r
        auto producer = do_create_producer(my_frame_factory, params);\r
        auto key_producer = frame_producer::empty();\r
@@ -119,17 +181,27 @@ safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& my
        catch(...){}\r
 \r
        if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
-               producer = create_separated_producer(producer, key_producer);\r
+               return create_separated_producer(producer, key_producer);\r
        \r
        if(producer == frame_producer::empty())\r
        {\r
                std::wstring str;\r
                BOOST_FOREACH(auto& param, params)\r
                        str += param + L" ";\r
-               BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(narrow(str)));\r
+               BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));\r
        }\r
 \r
        return producer;\r
 }\r
 \r
+\r
+spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, 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
+}\r
+\r
 }}
\ No newline at end of file