2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
24 #include "video_channel.h"
26 #include "video_format.h"
28 #include "producer/stage.h"
29 #include "mixer/mixer.h"
30 #include "consumer/output.h"
31 #include "frame/frame.h"
32 #include "frame/draw_frame.h"
33 #include "frame/frame_factory.h"
35 #include <common/diagnostics/graph.h>
36 #include <common/env.h>
37 #include <common/lock.h>
38 #include <common/executor.h>
40 #include <core/mixer/image/image_mixer.h>
41 #include <core/diagnostics/call_context.h>
43 #include <tbb/spin_mutex.h>
45 #include <boost/property_tree/ptree.hpp>
49 namespace caspar { namespace core {
51 struct video_channel::impl final
53 spl::shared_ptr<monitor::subject> monitor_subject_;
57 mutable tbb::spin_mutex format_desc_mutex_;
58 core::video_format_desc format_desc_;
60 const spl::shared_ptr<caspar::diagnostics::graph> graph_ = [](int index)
62 core::diagnostics::scoped_call_context save;
63 core::diagnostics::call_context::for_thread().video_channel = index;
64 return spl::make_shared<caspar::diagnostics::graph>();
67 caspar::core::output output_;
68 spl::shared_ptr<image_mixer> image_mixer_;
69 caspar::core::mixer mixer_;
70 caspar::core::stage stage_;
72 executor executor_ = L"video_channel";
74 impl(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer)
75 : monitor_subject_(spl::make_shared<monitor::subject>(
76 "/channel/" + boost::lexical_cast<std::string>(index)))
78 , format_desc_(format_desc)
79 , output_(graph_, format_desc, index)
80 , image_mixer_(std::move(image_mixer))
81 , mixer_(graph_, image_mixer_)
84 graph_->set_color("tick-time", caspar::diagnostics::color(0.0f, 0.6f, 0.9f));
85 graph_->set_text(print());
86 caspar::diagnostics::register_graph(graph_);
88 output_.monitor_output().attach_parent(monitor_subject_);
89 stage_.monitor_output().attach_parent(monitor_subject_);
91 executor_.begin_invoke([=]{tick();});
93 CASPAR_LOG(info) << print() << " Successfully Initialized.";
96 core::video_format_desc video_format_desc() const
98 return lock(format_desc_mutex_, [&]
104 void video_format_desc(const core::video_format_desc& format_desc)
106 lock(format_desc_mutex_, [&]
108 format_desc_ = format_desc;
118 auto format_desc = video_format_desc();
120 boost::timer frame_timer;
124 auto stage_frames = stage_(format_desc);
128 auto mixed_frame = mixer_(std::move(stage_frames), format_desc);
132 output_(std::move(mixed_frame), format_desc);
134 auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
135 graph_->set_value("tick-time", frame_time);
137 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc_.fps)
138 << monitor::message("/format") % format_desc.name;
142 CASPAR_LOG_CURRENT_EXCEPTION();
145 executor_.begin_invoke([=]{tick();});
148 std::wstring print() const
150 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + video_format_desc().name + L"]";
158 boost::property_tree::wptree info() const
160 boost::property_tree::wptree info;
162 auto stage_info = stage_.info();
163 auto mixer_info = mixer_.info();
164 auto output_info = output_.info();
166 info.add(L"video-mode", format_desc_.name);
167 info.add_child(L"stage", stage_info.get());
168 info.add_child(L"mixer", mixer_info.get());
169 info.add_child(L"output", output_info.get());
175 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))){}
176 video_channel::~video_channel(){}
177 const stage& video_channel::stage() const { return impl_->stage_;}
178 stage& video_channel::stage() { return impl_->stage_;}
179 const mixer& video_channel::mixer() const{ return impl_->mixer_;}
180 mixer& video_channel::mixer() { return impl_->mixer_;}
181 const output& video_channel::output() const { return impl_->output_;}
182 output& video_channel::output() { return impl_->output_;}
183 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;}
184 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
185 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
186 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
187 int video_channel::index() const { return impl_->index(); }
188 monitor::subject& video_channel::monitor_output(){ return *impl_->monitor_subject_; }