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"
34 #include "frame/audio_channel_layout.h"
36 #include <common/diagnostics/graph.h>
37 #include <common/env.h>
38 #include <common/lock.h>
39 #include <common/executor.h>
40 #include <common/timer.h>
42 #include <core/mixer/image/image_mixer.h>
43 #include <core/diagnostics/call_context.h>
45 #include <tbb/spin_mutex.h>
47 #include <boost/property_tree/ptree.hpp>
48 #include <boost/lexical_cast.hpp>
52 namespace caspar { namespace core {
54 struct video_channel::impl final
56 spl::shared_ptr<monitor::subject> monitor_subject_;
60 mutable tbb::spin_mutex format_desc_mutex_;
61 core::video_format_desc format_desc_;
62 mutable tbb::spin_mutex channel_layout_mutex_;
63 core::audio_channel_layout channel_layout_;
65 const spl::shared_ptr<caspar::diagnostics::graph> graph_ = [](int index)
67 core::diagnostics::scoped_call_context save;
68 core::diagnostics::call_context::for_thread().video_channel = index;
69 return spl::make_shared<caspar::diagnostics::graph>();
72 caspar::core::output output_;
73 spl::shared_ptr<image_mixer> image_mixer_;
74 caspar::core::mixer mixer_;
75 caspar::core::stage stage_;
77 executor executor_ { L"video_channel " + boost::lexical_cast<std::wstring>(index_) };
81 const core::video_format_desc& format_desc,
82 const core::audio_channel_layout& channel_layout,
83 std::unique_ptr<image_mixer> image_mixer)
84 : monitor_subject_(spl::make_shared<monitor::subject>(
85 "/channel/" + boost::lexical_cast<std::string>(index)))
87 , format_desc_(format_desc)
88 , channel_layout_(channel_layout)
89 , output_(graph_, format_desc, channel_layout, index)
90 , image_mixer_(std::move(image_mixer))
91 , mixer_(index, graph_, image_mixer_)
92 , stage_(index, graph_)
94 graph_->set_color("tick-time", caspar::diagnostics::color(0.0f, 0.6f, 0.9f));
95 graph_->set_text(print());
96 caspar::diagnostics::register_graph(graph_);
98 output_.monitor_output().attach_parent(monitor_subject_);
99 mixer_.monitor_output().attach_parent(monitor_subject_);
100 stage_.monitor_output().attach_parent(monitor_subject_);
102 executor_.begin_invoke([=]{tick();});
104 CASPAR_LOG(info) << print() << " Successfully Initialized.";
109 CASPAR_LOG(info) << print() << " Uninitializing.";
112 core::video_format_desc video_format_desc() const
114 return lock(format_desc_mutex_, [&]
120 void video_format_desc(const core::video_format_desc& format_desc)
122 lock(format_desc_mutex_, [&]
124 format_desc_ = format_desc;
129 core::audio_channel_layout audio_channel_layout() const
131 return lock(channel_layout_mutex_, [&]
133 return channel_layout_;
137 void audio_channel_layout(const core::audio_channel_layout& channel_layout)
139 lock(channel_layout_mutex_, [&]
141 channel_layout_ = channel_layout;
151 auto format_desc = video_format_desc();
152 auto channel_layout = audio_channel_layout();
154 caspar::timer frame_timer;
158 auto stage_frames = stage_(format_desc);
162 auto mixed_frame = mixer_(std::move(stage_frames), format_desc, channel_layout);
166 output_(std::move(mixed_frame), format_desc, channel_layout);
168 auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
169 graph_->set_value("tick-time", frame_time);
171 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc_.fps)
172 << monitor::message("/format") % format_desc.name;
176 CASPAR_LOG_CURRENT_EXCEPTION();
179 if (executor_.is_running())
180 executor_.begin_invoke([=]{tick();});
183 std::wstring print() const
185 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + video_format_desc().name + L"]";
193 boost::property_tree::wptree info() const
195 boost::property_tree::wptree info;
197 auto stage_info = stage_.info();
198 auto mixer_info = mixer_.info();
199 auto output_info = output_.info();
201 info.add(L"video-mode", video_format_desc().name);
202 info.add(L"audio-channel-layout", audio_channel_layout().print());
203 info.add_child(L"stage", stage_info.get());
204 info.add_child(L"mixer", mixer_info.get());
205 info.add_child(L"output", output_info.get());
210 boost::property_tree::wptree delay_info() const
212 boost::property_tree::wptree info;
214 auto stage_info = stage_.delay_info();
215 auto mixer_info = mixer_.delay_info();
216 auto output_info = output_.delay_info();
218 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
219 info.add_child(L"layers", stage_info.get());
220 info.add_child(L"mix-time", mixer_info.get());
221 info.add_child(L"output", output_info.get());
227 video_channel::video_channel(
229 const core::video_format_desc& format_desc,
230 const core::audio_channel_layout& channel_layout,
231 std::unique_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, channel_layout, std::move(image_mixer))){}
232 video_channel::~video_channel(){}
233 const stage& video_channel::stage() const { return impl_->stage_;}
234 stage& video_channel::stage() { return impl_->stage_;}
235 const mixer& video_channel::mixer() const{ return impl_->mixer_;}
236 mixer& video_channel::mixer() { return impl_->mixer_;}
237 const output& video_channel::output() const { return impl_->output_;}
238 output& video_channel::output() { return impl_->output_;}
239 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;}
240 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
241 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
242 core::audio_channel_layout video_channel::audio_channel_layout() const { return impl_->audio_channel_layout(); }
243 void core::video_channel::audio_channel_layout(const core::audio_channel_layout& channel_layout) { impl_->audio_channel_layout(channel_layout); }
244 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
245 boost::property_tree::wptree video_channel::delay_info() const { return impl_->delay_info(); }
246 int video_channel::index() const { return impl_->index(); }
247 monitor::subject& video_channel::monitor_output(){ return *impl_->monitor_subject_; }