]> git.sesse.net Git - casparcg/blobdiff - core/video_channel.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / video_channel.cpp
index cacfc4f3cab3e47576204a005d70f1c43434b659..6614f48567d43cf327ac472fd5456ddd048134b9 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\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
-*\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
-*/\r
-\r
-#include "StdAfx.h"\r
-\r
-#include "video_channel.h"\r
-\r
-#include "video_format.h"\r
-\r
-#include "consumer/output.h"\r
-#include "mixer/mixer.h"\r
-#include "producer/stage.h"\r
-\r
-#include <common/concurrency/executor.h>\r
-#include <common/diagnostics/graph.h>\r
-\r
-#include "mixer/gpu/ogl_device.h"\r
-\r
-#include <agents_extras.h>\r
-\r
-#include <boost/timer.hpp>\r
-\r
-#ifdef _MSC_VER\r
-#pragma warning(disable : 4355)\r
-#endif\r
-\r
-namespace caspar { namespace core {\r
-\r
-struct video_channel::implementation : boost::noncopyable\r
-{\r
-       Concurrency::unbounded_buffer<safe_ptr<message<std::map<int, safe_ptr<basic_frame>>>>>  stage_frames_;\r
-       Concurrency::unbounded_buffer<safe_ptr<message<safe_ptr<read_frame>>>>                                  mixer_frames_;\r
-       \r
-       const video_format_desc format_desc_;\r
-\r
-       safe_ptr<caspar::core::output>                  output_;\r
-       std::shared_ptr<caspar::core::mixer>    mixer_;\r
-       safe_ptr<caspar::core::stage>                   stage_;\r
-\r
-       safe_ptr<diagnostics::graph>    graph_;\r
-       boost::timer                                    frame_timer_;\r
-       boost::timer                                    tick_timer_;\r
-       boost::timer                                    output_timer_;\r
-       \r
-public:\r
-       implementation(int index, const video_format_desc& format_desc, ogl_device& ogl)  \r
-               : graph_(diagnostics::create_graph(narrow(print()), false))\r
-               , format_desc_(format_desc)\r
-               , output_(new caspar::core::output(mixer_frames_, format_desc))\r
-               , mixer_(new caspar::core::mixer(stage_frames_, mixer_frames_, format_desc, ogl))\r
-               , stage_(new caspar::core::stage(stage_frames_))        \r
-       {\r
-               graph_->add_guide("produce-time", 0.5f);        \r
-               graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
-               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
-               graph_->set_color("output-time", diagnostics::color(1.0f, 0.5f, 0.0f));\r
-               graph_->set_color("mix-time", diagnostics::color(1.0f, 1.0f, 0.9f));\r
-               graph_->start();\r
-\r
-               CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
-       }\r
-       \r
-       //void tick()\r
-       //{\r
-       //      try\r
-       //      {\r
-       //              // Produce\r
-\r
-       //              frame_timer_.restart();\r
-\r
-       //              auto simple_frames = stage_->execute();\r
-\r
-       //              graph_->update_value("produce-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
-       //      \r
-       //              // Mix\r
-\r
-       //              frame_timer_.restart();\r
-\r
-       //              auto finished_frame = mixer_->execute(simple_frames);\r
-       //      \r
-       //              graph_->update_value("mix-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
-       //      \r
-       //              // Consume\r
-       //      \r
-       //              output_timer_.restart();\r
-\r
-       //              output_->execute(finished_frame);\r
-       //      \r
-       //              graph_->update_value("output-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
-\r
-       //      \r
-       //              graph_->update_value("tick-time", tick_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
-       //              tick_timer_.restart();\r
-       //      }\r
-       //      catch(...)\r
-       //      {\r
-       //              CASPAR_LOG_CURRENT_EXCEPTION();\r
-       //              CASPAR_LOG(error) << context_.print() << L" Unexpected exception. Clearing stage and freeing memory";\r
-       //              restart();\r
-       //      }\r
-\r
-       //      context_.execution().begin_invoke([this]{tick();});\r
-       //}\r
-\r
-       //void restart()\r
-       //{\r
-       //      stage_->clear();\r
-       //      context_.ogl().gc().wait();\r
-\r
-       //      mixer_ = nullptr;\r
-       //      mixer_.reset(new caspar::core::mixer(context_));\r
-       //}\r
-               \r
-       std::wstring print() const\r
-       {\r
-               return L"video_channel";\r
-       }\r
-\r
-       //void set_video_format_desc(const video_format_desc& format_desc)\r
-       //{\r
-       //      context_.execution().begin_invoke([=]\r
-       //      {\r
-       //              stage_->clear();\r
-       //              context_.ogl().gc().wait();\r
-       //              context_.set_format_desc(format_desc);\r
-       //      });\r
-       //}\r
-};\r
-\r
-video_channel::video_channel(int index, const video_format_desc& format_desc, ogl_device& ogl) : impl_(new implementation(index, format_desc, ogl)){}\r
-video_channel::video_channel(video_channel&& other) : impl_(std::move(other.impl_)){}\r
-safe_ptr<stage> video_channel::stage() { return impl_->stage_;} \r
-safe_ptr<mixer> video_channel::mixer() { return make_safe_ptr(impl_->mixer_);} \r
-safe_ptr<output> video_channel::output() { return impl_->output_;} \r
-video_format_desc video_channel::get_video_format_desc() const{return impl_->format_desc_;}\r
-std::wstring video_channel::print() const { return impl_->print();}\r
-\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "StdAfx.h"
+
+#include "video_channel.h"
+
+#include "video_format.h"
+
+#include "producer/stage.h"
+#include "mixer/mixer.h"
+#include "consumer/output.h"
+#include "frame/frame.h"
+#include "frame/draw_frame.h"
+#include "frame/frame_factory.h"
+
+#include <common/diagnostics/graph.h>
+#include <common/env.h>
+#include <common/lock.h>
+#include <common/executor.h>
+
+#include <core/mixer/image/image_mixer.h>
+
+#include <tbb/spin_mutex.h>
+
+#include <boost/property_tree/ptree.hpp>
+
+#include <string>
+
+namespace caspar { namespace core {
+
+struct video_channel::impl sealed
+{
+       monitor::basic_subject                                                  event_subject_;
+
+       const int                                                                               index_;
+
+       mutable tbb::spin_mutex                                                 format_desc_mutex_;
+       core::video_format_desc                                                 format_desc_;
+       
+       const spl::shared_ptr<diagnostics::graph>               graph_;
+
+       caspar::core::output                                                    output_;
+       spl::shared_ptr<image_mixer>                                    image_mixer_;
+       caspar::core::mixer                                                             mixer_;
+       caspar::core::stage                                                             stage_; 
+
+       executor                                                                                executor_;
+public:
+       impl(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer)  
+               : event_subject_(monitor::path() % "channel" % index)
+               , index_(index)
+               , format_desc_(format_desc)
+               , output_(graph_, format_desc, index)
+               , image_mixer_(std::move(image_mixer))
+               , mixer_(graph_, image_mixer_)
+               , stage_(graph_)
+               , executor_(L"video_channel")
+       {
+               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   
+               graph_->set_text(print());
+               diagnostics::register_graph(graph_);
+               
+               output_.subscribe(event_subject_);
+               stage_.subscribe(event_subject_);
+
+               executor_.begin_invoke([=]{tick();});
+
+               CASPAR_LOG(info) << print() << " Successfully Initialized.";
+       }
+                                                       
+       core::video_format_desc video_format_desc() const
+       {
+               return lock(format_desc_mutex_, [&]
+               {
+                       return format_desc_;
+               });
+       }
+               
+       void video_format_desc(const core::video_format_desc& format_desc)
+       {
+               lock(format_desc_mutex_, [&]
+               {
+                       format_desc_ = format_desc;
+                       stage_.clear();
+               });
+       }
+
+       void tick()
+       {
+               try
+               {
+
+                       auto format_desc = video_format_desc();
+                       
+                       boost::timer frame_timer;
+
+                       // Produce
+                       
+                       auto stage_frames = stage_(format_desc);
+                       
+                       // Mix
+                       
+                       auto mixed_frame  = mixer_(std::move(stage_frames), format_desc);
+                       
+                       // Consume
+                                               
+                       output_(std::move(mixed_frame), format_desc);
+               
+                       graph_->set_value("tick-time", frame_timer.elapsed()*format_desc.fps*0.5);
+
+                       event_subject_  << monitor::event("profiler/time")      % frame_timer.elapsed() % (1.0/format_desc_.fps)
+                                                       << monitor::event("format")                     % format_desc.name;
+               }
+               catch(...)
+               {
+                       CASPAR_LOG_CURRENT_EXCEPTION();
+               }
+
+               executor_.begin_invoke([=]{tick();});
+       }
+                       
+       std::wstring print() const
+       {
+               return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";
+       }
+
+       boost::property_tree::wptree info() const
+       {
+               boost::property_tree::wptree info;
+
+               auto stage_info  = stage_.info();
+               auto mixer_info  = mixer_.info();
+               auto output_info = output_.info();
+
+               info.add(L"video-mode", format_desc_.name);
+               info.add_child(L"stage", stage_info.get());
+               info.add_child(L"mixer", mixer_info.get());
+               info.add_child(L"output", output_info.get());
+   
+               return info;                       
+       }
+};
+
+video_channel::video_channel(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, std::move(image_mixer))){}
+video_channel::~video_channel(){}
+const stage& video_channel::stage() const { return impl_->stage_;} 
+stage& video_channel::stage() { return impl_->stage_;} 
+const mixer& video_channel::mixer() const{ return impl_->mixer_;} 
+mixer& video_channel::mixer() { return impl_->mixer_;} 
+const output& video_channel::output() const { return impl_->output_;} 
+output& video_channel::output() { return impl_->output_;} 
+spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;} 
+core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
+void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
+boost::property_tree::wptree video_channel::info() const{return impl_->info();}                
+void video_channel::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
+void video_channel::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
+
 }}
\ No newline at end of file