]> git.sesse.net Git - casparcg/blobdiff - core/consumer/output.cpp
audio_mixer: Added some comments.
[casparcg] / core / consumer / output.cpp
index 4a2941c575d287f262bbca26c7df1bb8d55e94f1..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 key_read_frame_muxer : 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
-       key_read_frame_muxer(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
@@ -113,47 +91,60 @@ public:
        }\r
                                                \r
        void execute(const safe_ptr<read_frame>& frame)\r
-       {               \r
-               try\r
-               {               \r
-                       if(!has_synchronization_clock())\r
-                               timer_.tick(1.0/channel_.get_format_desc().fps);\r
-                                               \r
-                       auto fill = frame;\r
-                       auto key = make_safe<key_read_frame_muxer>(channel_.ogl(), frame);\r
+       {                       \r
+               if(!has_synchronization_clock())\r
+                       timer_.tick(1.0/channel_.get_format_desc().fps);\r
 \r
-                       auto it = consumers_.begin();\r
-                       while(it != consumers_.end())\r
+               if(frame->image_size() != channel_.get_format_desc().size)\r
+               {\r
+                       timer_.tick(1.0/channel_.get_format_desc().fps);\r
+                       return;\r
+               }\r
+               \r
+               auto it = consumers_.begin();\r
+               while(it != consumers_.end())\r
+               {\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
+\r
+                       try\r
+                       {\r
+                               if(consumer->send(frame))\r
+                                       ++it;\r
+                               else\r
+                                       consumers_.erase(it++);\r
+                       }\r
+                       catch(...)\r
                        {\r
+                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                               CASPAR_LOG(warning) << "Trying to restart consumer: " << consumer->print() << L".";\r
                                try\r
                                {\r
-                                       auto consumer = it->second;\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
-                                       if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
+                                       try\r
+                                       {\r
+                                               restart_channel_();\r
                                                consumer->initialize(channel_.get_format_desc());\r
-\r
-                                       auto frame = consumer->key_only() ? key : fill;\r
-\r
-                                       if(frame->image_size() == consumer->get_video_format_desc().size)\r
-                                       {       \r
-                                               if(!consumer->send(frame))\r
-                                                       consumers_.erase(it++);\r
-                                               else\r
-                                                       ++it;\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
-                               catch(...)\r
-                               {\r
-                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                                       CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
-                                       consumers_.erase(it++);\r
                                }\r
                        }\r
                }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
        }\r
 \r
 private:\r
@@ -172,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