-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\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
-*\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 "video_channel.h"\r
-\r
-#include "video_format.h"\r
-\r
-#include "consumer/output.h"\r
-#include "mixer/mixer.h"\r
-#include "mixer/write_frame.h"\r
-#include "mixer/gpu/accelerator.h"\r
-#include "frame/data_frame.h"\r
-#include "producer/stage.h"\r
-#include "frame/frame_factory.h"\r
-\r
-#include <common/diagnostics/graph.h>\r
-#include <common/env.h>\r
-#include <common/concurrency/lock.h>\r
-\r
-#include <tbb/spin_mutex.h>\r
-\r
-#include <boost/property_tree/ptree.hpp>\r
-\r
-#include <string>\r
-\r
-namespace caspar { namespace core {\r
-\r
-struct video_channel::impl sealed : public frame_factory\r
-{\r
- reactive::basic_subject<safe_ptr<const data_frame>> frame_subject_;\r
- const int index_;\r
-\r
- mutable tbb::spin_mutex format_desc_mutex_;\r
- video_format_desc format_desc_;\r
- \r
- const safe_ptr<gpu::accelerator> ogl_;\r
- const safe_ptr<diagnostics::graph> graph_;\r
-\r
- const safe_ptr<caspar::core::output> output_;\r
- const safe_ptr<caspar::core::mixer> mixer_;\r
- const safe_ptr<caspar::core::stage> stage_; \r
-\r
- boost::timer tick_timer_;\r
- boost::timer produce_timer_;\r
- boost::timer mix_timer_;\r
- boost::timer consume_timer_;\r
-\r
- executor executor_;\r
-public:\r
- impl(int index, const video_format_desc& format_desc, const safe_ptr<gpu::accelerator>& ogl) \r
- : index_(index)\r
- , format_desc_(format_desc)\r
- , ogl_(ogl)\r
- , output_(new caspar::core::output(format_desc, index))\r
- , mixer_(new caspar::core::mixer(ogl))\r
- , stage_(new caspar::core::stage()) \r
- , executor_(L"video_channel")\r
- {\r
- //graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));\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("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));\r
- graph_->set_text(print());\r
- diagnostics::register_graph(graph_);\r
-\r
- executor_.begin_invoke([=]{tick();});\r
-\r
- CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
- }\r
- \r
- // frame_factory\r
- \r
- virtual safe_ptr<write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc) override\r
- { \r
- return make_safe<write_frame>(ogl_, tag, desc);\r
- }\r
- \r
- virtual core::video_format_desc get_video_format_desc() const override\r
- {\r
- return lock(format_desc_mutex_, [&]\r
- {\r
- return format_desc_;\r
- });\r
- }\r
- \r
- // video_channel\r
-\r
- void tick()\r
- {\r
- tick_timer_.restart();\r
-\r
- // Produce\r
-\r
- produce_timer_.restart();\r
-\r
- auto stage_frames = (*stage_)(format_desc_);\r
- \r
- graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
-\r
- // Mix\r
-\r
- //mix_timer_.restart();\r
-\r
- auto mixed_frame = (*mixer_)(std::move(stage_frames), format_desc_);\r
- \r
- //graph_->set_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);\r
-\r
- // Consume\r
-\r
- consume_timer_.restart();\r
-\r
- frame_subject_.on_next(mixed_frame);\r
-\r
- graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
-\r
- (*output_)(std::move(mixed_frame), format_desc_);\r
- \r
- graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
-\r
- executor_.begin_invoke([=]{tick();});\r
- }\r
-\r
- void set_video_format_desc(const video_format_desc& format_desc)\r
- {\r
- ogl_->gc();\r
- lock(format_desc_mutex_, [&]\r
- {\r
- format_desc_ = format_desc;\r
- });\r
- }\r
- \r
- std::wstring print() const\r
- {\r
- return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + format_desc_.name + L"]";\r
- }\r
-\r
- boost::property_tree::wptree info() const\r
- {\r
- boost::property_tree::wptree info;\r
-\r
- auto stage_info = stage_->info();\r
- auto mixer_info = mixer_->info();\r
- auto output_info = output_->info();\r
-\r
- stage_info.timed_wait(boost::posix_time::seconds(2));\r
- mixer_info.timed_wait(boost::posix_time::seconds(2));\r
- output_info.timed_wait(boost::posix_time::seconds(2));\r
- \r
- info.add(L"video-mode", format_desc_.name);\r
- info.add_child(L"stage", stage_info.get());\r
- info.add_child(L"mixer", mixer_info.get());\r
- info.add_child(L"output", output_info.get());\r
- \r
- return info; \r
- }\r
-};\r
-\r
-video_channel::video_channel(int index, const video_format_desc& format_desc, const safe_ptr<gpu::accelerator>& ogl) : impl_(new impl(index, format_desc, ogl)){}\r
-safe_ptr<stage> video_channel::stage() { return impl_->stage_;} \r
-safe_ptr<mixer> video_channel::mixer() { return impl_->mixer_;} \r
-safe_ptr<frame_factory> video_channel::frame_factory() { return impl_;} \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
-void video_channel::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
-boost::property_tree::wptree video_channel::info() const{return impl_->info();}\r
-void video_channel::subscribe(const observer_ptr& o) {impl_->frame_subject_.subscribe(o);}\r
-void video_channel::unsubscribe(const observer_ptr& o) {impl_->frame_subject_.unsubscribe(o);}\r
-\r
-}}
\ No newline at end of file
+/*
+* 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 "frame/audio_channel_layout.h"
+
+#include <common/diagnostics/graph.h>
+#include <common/env.h>
+#include <common/lock.h>
+#include <common/executor.h>
+#include <common/timer.h>
+#include <common/future.h>
+
+#include <core/mixer/image/image_mixer.h>
+#include <core/diagnostics/call_context.h>
+
+#include <tbb/spin_mutex.h>
+
+#include <boost/property_tree/ptree.hpp>
+#include <boost/lexical_cast.hpp>
+
+#include <string>
+
+namespace caspar { namespace core {
+
+struct video_channel::impl final
+{
+ spl::shared_ptr<monitor::subject> monitor_subject_;
+
+ const int index_;
+
+ mutable tbb::spin_mutex format_desc_mutex_;
+ core::video_format_desc format_desc_;
+ mutable tbb::spin_mutex channel_layout_mutex_;
+ core::audio_channel_layout channel_layout_;
+
+ const spl::shared_ptr<caspar::diagnostics::graph> graph_ = [](int index)
+ {
+ core::diagnostics::scoped_call_context save;
+ core::diagnostics::call_context::for_thread().video_channel = index;
+ return spl::make_shared<caspar::diagnostics::graph>();
+ }(index_);
+
+ caspar::core::output output_;
+ std::future<void> output_ready_for_frame_ = make_ready_future();
+ spl::shared_ptr<image_mixer> image_mixer_;
+ caspar::core::mixer mixer_;
+ caspar::core::stage stage_;
+
+ executor executor_ { L"video_channel " + boost::lexical_cast<std::wstring>(index_) };
+public:
+ impl(
+ int index,
+ const core::video_format_desc& format_desc,
+ const core::audio_channel_layout& channel_layout,
+ std::unique_ptr<image_mixer> image_mixer)
+ : monitor_subject_(spl::make_shared<monitor::subject>(
+ "/channel/" + boost::lexical_cast<std::string>(index)))
+ , index_(index)
+ , format_desc_(format_desc)
+ , channel_layout_(channel_layout)
+ , output_(graph_, format_desc, channel_layout, index)
+ , image_mixer_(std::move(image_mixer))
+ , mixer_(index, graph_, image_mixer_)
+ , stage_(index, graph_)
+ {
+ graph_->set_color("tick-time", caspar::diagnostics::color(0.0f, 0.6f, 0.9f));
+ graph_->set_text(print());
+ caspar::diagnostics::register_graph(graph_);
+
+ output_.monitor_output().attach_parent(monitor_subject_);
+ mixer_.monitor_output().attach_parent(monitor_subject_);
+ stage_.monitor_output().attach_parent(monitor_subject_);
+
+ executor_.begin_invoke([=]{tick();});
+
+ CASPAR_LOG(info) << print() << " Successfully Initialized.";
+ }
+
+ ~impl()
+ {
+ CASPAR_LOG(info) << print() << " Uninitializing.";
+ }
+
+ 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();
+ });
+ }
+
+ core::audio_channel_layout audio_channel_layout() const
+ {
+ return lock(channel_layout_mutex_, [&]
+ {
+ return channel_layout_;
+ });
+ }
+
+ void audio_channel_layout(const core::audio_channel_layout& channel_layout)
+ {
+ lock(channel_layout_mutex_, [&]
+ {
+ channel_layout_ = channel_layout;
+ stage_.clear();
+ });
+ }
+
+ void tick()
+ {
+ try
+ {
+
+ auto format_desc = video_format_desc();
+ auto channel_layout = audio_channel_layout();
+
+ caspar::timer frame_timer;
+
+ // Produce
+
+ auto stage_frames = stage_(format_desc);
+
+ // Mix
+
+ auto mixed_frame = mixer_(std::move(stage_frames), format_desc, channel_layout);
+
+ // Consume
+
+ output_ready_for_frame_.get();
+ output_ready_for_frame_ = output_(std::move(mixed_frame), format_desc, channel_layout);
+
+ auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
+ graph_->set_value("tick-time", frame_time);
+
+ *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc_.fps)
+ << monitor::message("/format") % format_desc.name;
+ }
+ catch(...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ }
+
+ if (executor_.is_running())
+ executor_.begin_invoke([=]{tick();});
+ }
+
+ std::wstring print() const
+ {
+ return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + video_format_desc().name + L"]";
+ }
+
+ int index() const
+ {
+ return index_;
+ }
+
+ 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", video_format_desc().name);
+ info.add(L"audio-channel-layout", audio_channel_layout().print());
+ 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;
+ }
+
+ boost::property_tree::wptree delay_info() const
+ {
+ boost::property_tree::wptree info;
+
+ auto stage_info = stage_.delay_info();
+ auto mixer_info = mixer_.delay_info();
+ auto output_info = output_.delay_info();
+
+ // TODO: because of std::async deferred timed waiting does not work so for now we have to block
+ info.add_child(L"layers", stage_info.get());
+ info.add_child(L"mix-time", 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,
+ const core::audio_channel_layout& channel_layout,
+ std::unique_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, channel_layout, 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);}
+core::audio_channel_layout video_channel::audio_channel_layout() const { return impl_->audio_channel_layout(); }
+void core::video_channel::audio_channel_layout(const core::audio_channel_layout& channel_layout) { impl_->audio_channel_layout(channel_layout); }
+boost::property_tree::wptree video_channel::info() const{return impl_->info();}
+boost::property_tree::wptree video_channel::delay_info() const { return impl_->delay_info(); }
+int video_channel::index() const { return impl_->index(); }
+monitor::subject& video_channel::monitor_output(){ return *impl_->monitor_subject_; }
+
+}}