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 "producer/stage.h"
\r
29 #include "mixer/mixer.h"
\r
30 #include "consumer/output.h"
\r
31 #include "frame/data_frame.h"
\r
32 #include "frame/frame_factory.h"
\r
34 #include <common/diagnostics/graph.h>
\r
35 #include <common/env.h>
\r
36 #include <common/concurrency/lock.h>
\r
37 #include <common/concurrency/executor.h>
\r
39 #include <tbb/spin_mutex.h>
\r
41 #include <boost/property_tree/ptree.hpp>
\r
45 namespace caspar { namespace core {
\r
47 struct video_channel::impl sealed : public frame_factory
\r
49 reactive::basic_subject<spl::shared_ptr<const data_frame>> frame_subject_;
\r
50 spl::shared_ptr<monitor::subject> event_subject_;
\r
54 mutable tbb::spin_mutex format_desc_mutex_;
\r
55 video_format_desc format_desc_;
\r
57 const spl::shared_ptr<diagnostics::graph> graph_;
\r
59 const spl::shared_ptr<caspar::core::output> output_;
\r
60 const spl::shared_ptr<caspar::core::mixer> mixer_;
\r
61 const spl::shared_ptr<caspar::core::stage> stage_;
\r
65 impl(int index, const video_format_desc& format_desc, spl::shared_ptr<image_mixer> image_mixer)
\r
66 : event_subject_(new monitor::subject(monitor::path() % "channel" % index))
\r
68 , format_desc_(format_desc)
\r
69 , output_(new caspar::core::output(graph_, format_desc, index))
\r
70 , mixer_(new caspar::core::mixer(graph_, std::move(image_mixer)))
\r
71 , stage_(new caspar::core::stage(graph_))
\r
72 , executor_(L"video_channel")
\r
74 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
\r
75 graph_->set_text(print());
\r
76 diagnostics::register_graph(graph_);
\r
78 stage_->subscribe(event_subject_);
\r
80 executor_.begin_invoke([=]{tick();});
\r
82 CASPAR_LOG(info) << print() << " Successfully Initialized.";
\r
87 virtual spl::shared_ptr<write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc) override
\r
89 return mixer_->create_frame(tag, desc);
\r
92 virtual core::video_format_desc get_video_format_desc() const override
\r
94 return lock(format_desc_mutex_, [&]
\r
96 return format_desc_;
\r
106 auto format_desc = get_video_format_desc();
\r
108 boost::timer frame_timer;
\r
112 auto stage_frames = (*stage_)(format_desc);
\r
116 auto mixed_frame = (*mixer_)(std::move(stage_frames), format_desc);
\r
120 frame_subject_ << mixed_frame;
\r
122 (*output_)(std::move(mixed_frame), format_desc);
\r
124 graph_->set_value("tick-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
126 //*event_subject_ << monitor::event("debug/profiler") % frame_timer.elapsed() % (1.0/format_desc_.fps);
\r
127 //*event_subject_ << monitor::event("format") % u8(format_desc.name);
\r
131 CASPAR_LOG_CURRENT_EXCEPTION();
\r
134 executor_.begin_invoke([=]{tick();});
\r
137 void set_video_format_desc(const video_format_desc& format_desc)
\r
139 lock(format_desc_mutex_, [&]
\r
141 format_desc_ = format_desc;
\r
145 std::wstring print() const
\r
147 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" + get_video_format_desc().name + L"]";
\r
150 boost::property_tree::wptree info() const
\r
152 boost::property_tree::wptree info;
\r
154 auto stage_info = stage_->info();
\r
155 auto mixer_info = mixer_->info();
\r
156 auto output_info = output_->info();
\r
158 info.add(L"video-mode", format_desc_.name);
\r
159 info.add_child(L"stage", stage_info.get());
\r
160 info.add_child(L"mixer", mixer_info.get());
\r
161 info.add_child(L"output", output_info.get());
\r
167 video_channel::video_channel(int index, const video_format_desc& format_desc, spl::shared_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, image_mixer)){}
\r
168 spl::shared_ptr<stage> video_channel::stage() { return impl_->stage_;}
\r
169 spl::shared_ptr<mixer> video_channel::mixer() { return impl_->mixer_;}
\r
170 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_;}
\r
171 spl::shared_ptr<output> video_channel::output() { return impl_->output_;}
\r
172 video_format_desc video_channel::get_video_format_desc() const{return impl_->format_desc_;}
\r
173 void video_channel::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
174 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
\r
175 void video_channel::subscribe(const frame_observer::observer_ptr& o) {impl_->frame_subject_.subscribe(o);}
\r
176 void video_channel::unsubscribe(const frame_observer::observer_ptr& o) {impl_->frame_subject_.unsubscribe(o);}
\r
177 void video_channel::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->subscribe(o);}
\r
178 void video_channel::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->unsubscribe(o);}
\r