2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
24 #include "video_channel.h"
\r
26 #include "video_format.h"
\r
28 #include "consumer/output.h"
\r
29 #include "mixer/mixer.h"
\r
30 #include "mixer/gpu/write_frame.h"
\r
31 #include "mixer/gpu/accelerator.h"
\r
32 #include "frame/data_frame.h"
\r
33 #include "producer/stage.h"
\r
34 #include "frame/frame_factory.h"
\r
36 #include <common/diagnostics/graph.h>
\r
37 #include <common/env.h>
\r
38 #include <common/concurrency/lock.h>
\r
40 #include <tbb/spin_mutex.h>
\r
42 #include <boost/property_tree/ptree.hpp>
\r
46 namespace caspar { namespace core {
\r
48 struct video_channel::impl sealed : public frame_factory
\r
50 reactive::basic_subject<safe_ptr<const data_frame>> frame_subject_;
\r
53 mutable tbb::spin_mutex format_desc_mutex_;
\r
54 video_format_desc format_desc_;
\r
56 const safe_ptr<gpu::accelerator> ogl_;
\r
57 const safe_ptr<diagnostics::graph> graph_;
\r
59 const safe_ptr<caspar::core::output> output_;
\r
60 const safe_ptr<caspar::core::mixer> mixer_;
\r
61 const safe_ptr<caspar::core::stage> stage_;
\r
63 boost::timer tick_timer_;
\r
64 boost::timer produce_timer_;
\r
65 boost::timer mix_timer_;
\r
66 boost::timer consume_timer_;
\r
70 impl(int index, const video_format_desc& format_desc, const safe_ptr<gpu::accelerator>& ogl)
\r
72 , format_desc_(format_desc)
\r
74 , output_(new caspar::core::output(format_desc, index))
\r
75 , mixer_(new caspar::core::mixer(ogl))
\r
76 , stage_(new caspar::core::stage())
\r
77 , executor_(L"video_channel")
\r
79 //graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));
\r
80 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
81 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
82 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
\r
83 graph_->set_text(print());
\r
84 diagnostics::register_graph(graph_);
\r
86 executor_.begin_invoke([=]{tick();});
\r
88 CASPAR_LOG(info) << print() << " Successfully Initialized.";
\r
93 virtual safe_ptr<write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc) override
\r
95 return make_safe<write_frame>(ogl_, tag, desc);
\r
98 virtual core::video_format_desc get_video_format_desc() const override
\r
100 return lock(format_desc_mutex_, [&]
\r
102 return format_desc_;
\r
110 tick_timer_.restart();
\r
114 produce_timer_.restart();
\r
116 auto stage_frames = (*stage_)(format_desc_);
\r
118 graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);
\r
122 //mix_timer_.restart();
\r
124 auto mixed_frame = (*mixer_)(std::move(stage_frames), format_desc_);
\r
126 //graph_->set_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);
\r
130 consume_timer_.restart();
\r
132 frame_subject_.on_next(mixed_frame);
\r
134 graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);
\r
136 (*output_)(std::move(mixed_frame), format_desc_);
\r
138 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
140 executor_.begin_invoke([=]{tick();});
\r
143 void set_video_format_desc(const video_format_desc& format_desc)
\r
146 lock(format_desc_mutex_, [&]
\r
148 format_desc_ = format_desc;
\r
152 std::wstring print() const
\r
154 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + format_desc_.name + L"]";
\r
157 boost::property_tree::wptree info() const
\r
159 boost::property_tree::wptree info;
\r
161 auto stage_info = stage_->info();
\r
162 auto mixer_info = mixer_->info();
\r
163 auto output_info = output_->info();
\r
165 stage_info.timed_wait(boost::posix_time::seconds(2));
\r
166 mixer_info.timed_wait(boost::posix_time::seconds(2));
\r
167 output_info.timed_wait(boost::posix_time::seconds(2));
\r
169 info.add(L"video-mode", format_desc_.name);
\r
170 info.add_child(L"stage", stage_info.get());
\r
171 info.add_child(L"mixer", mixer_info.get());
\r
172 info.add_child(L"output", output_info.get());
\r
178 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
179 safe_ptr<stage> video_channel::stage() { return impl_->stage_;}
\r
180 safe_ptr<mixer> video_channel::mixer() { return impl_->mixer_;}
\r
181 safe_ptr<frame_factory> video_channel::frame_factory() { return impl_;}
\r
182 safe_ptr<output> video_channel::output() { return impl_->output_;}
\r
183 video_format_desc video_channel::get_video_format_desc() const{return impl_->format_desc_;}
\r
184 void video_channel::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
185 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
\r
186 void video_channel::subscribe(const observer_ptr& o) {impl_->frame_subject_.subscribe(o);}
\r
187 void video_channel::unsubscribe(const observer_ptr& o) {impl_->frame_subject_.unsubscribe(o);}
\r