]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
Refactored to use non-static data member initializers where it makes sense. Mixes...
[casparcg] / core / video_channel.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "StdAfx.h"
23
24 #include "video_channel.h"
25
26 #include "video_format.h"
27
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
35 #include <common/diagnostics/graph.h>
36 #include <common/env.h>
37 #include <common/lock.h>
38 #include <common/executor.h>
39
40 #include <core/mixer/image/image_mixer.h>
41
42 #include <tbb/spin_mutex.h>
43
44 #include <boost/property_tree/ptree.hpp>
45
46 #include <string>
47
48 namespace caspar { namespace core {
49
50 struct video_channel::impl final
51 {
52         spl::shared_ptr<monitor::subject>                               monitor_subject_;
53
54         const int                                                                               index_;
55
56         mutable tbb::spin_mutex                                                 format_desc_mutex_;
57         core::video_format_desc                                                 format_desc_;
58         
59         const spl::shared_ptr<diagnostics::graph>               graph_;
60
61         caspar::core::output                                                    output_;
62         spl::shared_ptr<image_mixer>                                    image_mixer_;
63         caspar::core::mixer                                                             mixer_;
64         caspar::core::stage                                                             stage_; 
65
66         executor                                                                                executor_                       = L"video_channel";
67 public:
68         impl(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer)  
69                 : monitor_subject_(spl::make_shared<monitor::subject>(
70                                 "/channel/" + boost::lexical_cast<std::string>(index)))
71                 , index_(index)
72                 , format_desc_(format_desc)
73                 , output_(graph_, format_desc, index)
74                 , image_mixer_(std::move(image_mixer))
75                 , mixer_(graph_, image_mixer_)
76                 , stage_(graph_)
77         {
78                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   
79                 graph_->set_text(print());
80                 diagnostics::register_graph(graph_);
81                 
82                 output_.monitor_output().attach_parent(monitor_subject_);
83                 stage_.monitor_output().attach_parent(monitor_subject_);
84
85                 executor_.begin_invoke([=]{tick();});
86
87                 CASPAR_LOG(info) << print() << " Successfully Initialized.";
88         }
89                                                         
90         core::video_format_desc video_format_desc() const
91         {
92                 return lock(format_desc_mutex_, [&]
93                 {
94                         return format_desc_;
95                 });
96         }
97                 
98         void video_format_desc(const core::video_format_desc& format_desc)
99         {
100                 lock(format_desc_mutex_, [&]
101                 {
102                         format_desc_ = format_desc;
103                         stage_.clear();
104                 });
105         }
106
107         void tick()
108         {
109                 try
110                 {
111
112                         auto format_desc = video_format_desc();
113                         
114                         boost::timer frame_timer;
115
116                         // Produce
117                         
118                         auto stage_frames = stage_(format_desc);
119                         
120                         // Mix
121                         
122                         auto mixed_frame  = mixer_(std::move(stage_frames), format_desc);
123                         
124                         // Consume
125                                                 
126                         output_(std::move(mixed_frame), format_desc);
127                 
128                         graph_->set_value("tick-time", frame_timer.elapsed()*format_desc.fps*0.5);
129
130                         *monitor_subject_       << monitor::message("/profiler/time")   % frame_timer.elapsed() % (1.0/format_desc_.fps)
131                                                                 << monitor::message("/format")                  % format_desc.name;
132                 }
133                 catch(...)
134                 {
135                         CASPAR_LOG_CURRENT_EXCEPTION();
136                 }
137
138                 executor_.begin_invoke([=]{tick();});
139         }
140                         
141         std::wstring print() const
142         {
143                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";
144         }
145
146         boost::property_tree::wptree info() const
147         {
148                 boost::property_tree::wptree info;
149
150                 auto stage_info  = stage_.info();
151                 auto mixer_info  = mixer_.info();
152                 auto output_info = output_.info();
153
154                 info.add(L"video-mode", format_desc_.name);
155                 info.add_child(L"stage", stage_info.get());
156                 info.add_child(L"mixer", mixer_info.get());
157                 info.add_child(L"output", output_info.get());
158    
159                 return info;                       
160         }
161 };
162
163 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))){}
164 video_channel::~video_channel(){}
165 const stage& video_channel::stage() const { return impl_->stage_;} 
166 stage& video_channel::stage() { return impl_->stage_;} 
167 const mixer& video_channel::mixer() const{ return impl_->mixer_;} 
168 mixer& video_channel::mixer() { return impl_->mixer_;} 
169 const output& video_channel::output() const { return impl_->output_;} 
170 output& video_channel::output() { return impl_->output_;} 
171 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;} 
172 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
173 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
174 boost::property_tree::wptree video_channel::info() const{return impl_->info();}         
175 monitor::subject& video_channel::monitor_output(){return *impl_->monitor_subject_;}
176
177 }}