]> git.sesse.net Git - casparcg/blobdiff - core/consumer/output.cpp
2.0. stage: Changed error handling.
[casparcg] / core / consumer / output.cpp
index 51b62e57620c5d41238f6fafc2135280b7eb9358..426013cac5e63790167c084bbfa36fac45b788ce 100644 (file)
@@ -17,6 +17,7 @@
 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
 *\r
 */\r
+// TODO: Try to recover consumer from bad_alloc...\r
 #include "../StdAfx.h"\r
 \r
 #ifdef _MSC_VER\r
 \r
 namespace caspar { namespace core {\r
 \r
-class deferred_key_read_Frame : public core::read_frame\r
-{\r
-       ogl_device&                                              ogl_;\r
-       safe_ptr<read_frame>                     fill_;\r
-       std::shared_ptr<host_buffer>     key_;\r
-       tbb::mutex                                           mutex_;\r
-public:\r
-       deferred_key_read_Frame(ogl_device& ogl, const safe_ptr<read_frame>& fill)\r
-               : ogl_(ogl)\r
-               , fill_(fill)\r
-       {\r
-       }\r
-\r
-       virtual const boost::iterator_range<const uint8_t*> image_data()\r
-       {\r
-               tbb::mutex::scoped_lock lock(mutex_);\r
-               if(!key_)\r
-               {\r
-                       key_ = ogl_.create_host_buffer(fill_->image_data().size(), host_buffer::write_only);                            \r
-                       fast_memsfhl(key_->data(), fill_->image_data().begin(), fill_->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
-               }\r
-\r
-               auto ptr = static_cast<const uint8_t*>(key_->data());\r
-               return boost::iterator_range<const uint8_t*>(ptr, ptr + key_->size());\r
-       }\r
-\r
-       virtual const boost::iterator_range<const int16_t*> audio_data()\r
-       {\r
-               return fill_->audio_data();\r
-       }       \r
-};\r
-       \r
 struct output::implementation\r
 {      \r
        typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
        \r
-       video_channel_context& channel_;\r
+       video_channel_context&          channel_;\r
+       const std::function<void()> restart_channel_;\r
 \r
        std::map<int, safe_ptr<frame_consumer>> consumers_;\r
        typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
@@ -84,15 +54,23 @@ struct output::implementation
        high_prec_timer timer_;\r
                \r
 public:\r
-       implementation(video_channel_context& video_channel) \r
-               : channel_(video_channel){}     \r
+       implementation(video_channel_context& video_channel, const std::function<void()>& restart_channel) \r
+               : channel_(video_channel)\r
+               , restart_channel_(restart_channel)\r
+       {\r
+       }       \r
        \r
        void add(int index, safe_ptr<frame_consumer>&& consumer)\r
        {               \r
-               consumer->initialize(channel_.get_format_desc());\r
                channel_.execution().invoke([&]\r
                {\r
                        consumers_.erase(index);\r
+               });\r
+\r
+               consumer->initialize(channel_.get_format_desc());\r
+\r
+               channel_.execution().invoke([&]\r
+               {\r
                        consumers_.insert(std::make_pair(index, consumer));\r
 \r
                        CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
@@ -122,21 +100,18 @@ public:
                        timer_.tick(1.0/channel_.get_format_desc().fps);\r
                        return;\r
                }\r
-\r
-               auto fill = frame;\r
-               auto key = make_safe<deferred_key_read_Frame>(channel_.ogl(), frame);\r
-\r
+               \r
                auto it = consumers_.begin();\r
                while(it != consumers_.end())\r
                {\r
-                       try\r
-                       {\r
-                               auto consumer = it->second;\r
+                       auto consumer = it->second;\r
 \r
-                               if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
-                                       consumer->initialize(channel_.get_format_desc());\r
+                       if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
+                               consumer->initialize(channel_.get_format_desc());\r
 \r
-                               if(consumer->send(consumer->key_only() ? key : fill))\r
+                       try\r
+                       {\r
+                               if(consumer->send(frame))\r
                                        ++it;\r
                                else\r
                                        consumers_.erase(it++);\r
@@ -144,8 +119,30 @@ public:
                        catch(...)\r
                        {\r
                                CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
-                               consumers_.erase(it++);\r
+                               CASPAR_LOG(warning) << "Trying to restart consumer: " << consumer->print() << L".";\r
+                               try\r
+                               {\r
+                                       consumer->initialize(channel_.get_format_desc());\r
+                                       consumer->send(frame);\r
+                               }\r
+                               catch(...)\r
+                               {       \r
+                                       CASPAR_LOG_CURRENT_EXCEPTION(); \r
+                                       CASPAR_LOG(warning) << "Consumer restart failed, trying to restart channel: " << consumer->print() << L".";     \r
+\r
+                                       try\r
+                                       {\r
+                                               restart_channel_();\r
+                                               consumer->initialize(channel_.get_format_desc());\r
+                                               consumer->send(frame);\r
+                                       }\r
+                                       catch(...)\r
+                                       {\r
+                                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                                               CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
+                                               consumers_.erase(it++);\r
+                                       }\r
+                               }\r
                        }\r
                }\r
        }\r
@@ -166,7 +163,7 @@ private:
        }\r
 };\r
 \r
-output::output(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
+output::output(video_channel_context& video_channel, const std::function<void()>& restart_channel) : impl_(new implementation(video_channel, restart_channel)){}\r
 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
 void output::remove(int index){impl_->remove(index);}\r
 void output::execute(const safe_ptr<read_frame>& frame) {impl_->execute(frame); }\r