]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
Merged OSC audio levels sending
[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 #include <common/timer.h>
40
41 #include <core/mixer/image/image_mixer.h>
42 #include <core/diagnostics/call_context.h>
43
44 #include <tbb/spin_mutex.h>
45
46 #include <boost/property_tree/ptree.hpp>
47 #include <boost/lexical_cast.hpp>
48
49 #include <string>
50
51 namespace caspar { namespace core {
52
53 struct video_channel::impl final
54 {
55         spl::shared_ptr<monitor::subject>                                       monitor_subject_;
56
57         const int                                                                                       index_;
58
59         mutable tbb::spin_mutex                                                         format_desc_mutex_;
60         core::video_format_desc                                                         format_desc_;
61         
62         const spl::shared_ptr<caspar::diagnostics::graph>       graph_                          = [](int index)
63                                                                                                                                                           {
64                                                                                                                                                                   core::diagnostics::scoped_call_context save;
65                                                                                                                                                                   core::diagnostics::call_context::for_thread().video_channel = index;
66                                                                                                                                                                   return spl::make_shared<caspar::diagnostics::graph>();
67                                                                                                                                                           }(index_);
68
69         caspar::core::output                                                            output_;
70         spl::shared_ptr<image_mixer>                                            image_mixer_;
71         caspar::core::mixer                                                                     mixer_;
72         caspar::core::stage                                                                     stage_; 
73
74         executor                                                                                        executor_                       { L"video_channel " + boost::lexical_cast<std::wstring>(index_) };
75 public:
76         impl(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer)  
77                 : monitor_subject_(spl::make_shared<monitor::subject>(
78                                 "/channel/" + boost::lexical_cast<std::string>(index)))
79                 , index_(index)
80                 , format_desc_(format_desc)
81                 , output_(graph_, format_desc, index)
82                 , image_mixer_(std::move(image_mixer))
83                 , mixer_(index, graph_, image_mixer_)
84                 , stage_(index, graph_)
85         {
86                 graph_->set_color("tick-time", caspar::diagnostics::color(0.0f, 0.6f, 0.9f));
87                 graph_->set_text(print());
88                 caspar::diagnostics::register_graph(graph_);
89
90                 output_.monitor_output().attach_parent(monitor_subject_);
91                 mixer_.monitor_output().attach_parent(monitor_subject_);
92                 stage_.monitor_output().attach_parent(monitor_subject_);
93
94                 executor_.begin_invoke([=]{tick();});
95
96                 CASPAR_LOG(info) << print() << " Successfully Initialized.";
97         }
98
99         ~impl()
100         {
101                 CASPAR_LOG(info) << print() << " Uninitializing.";
102         }
103                                                         
104         core::video_format_desc video_format_desc() const
105         {
106                 return lock(format_desc_mutex_, [&]
107                 {
108                         return format_desc_;
109                 });
110         }
111                 
112         void video_format_desc(const core::video_format_desc& format_desc)
113         {
114                 lock(format_desc_mutex_, [&]
115                 {
116                         format_desc_ = format_desc;
117                         stage_.clear();
118                 });
119         }
120
121         void tick()
122         {
123                 try
124                 {
125
126                         auto format_desc = video_format_desc();
127                         
128                         caspar::timer frame_timer;
129
130                         // Produce
131                         
132                         auto stage_frames = stage_(format_desc);
133                         
134                         // Mix
135                         
136                         auto mixed_frame  = mixer_(std::move(stage_frames), format_desc);
137                         
138                         // Consume
139                                                 
140                         output_(std::move(mixed_frame), format_desc);
141                 
142                         auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
143                         graph_->set_value("tick-time", frame_time);
144
145                         *monitor_subject_       << monitor::message("/profiler/time")   % frame_timer.elapsed() % (1.0/format_desc_.fps)
146                                                                 << monitor::message("/format")                  % format_desc.name;
147                 }
148                 catch(...)
149                 {
150                         CASPAR_LOG_CURRENT_EXCEPTION();
151                 }
152
153                 if (executor_.is_running())
154                         executor_.begin_invoke([=]{tick();});
155         }
156                         
157         std::wstring print() const
158         {
159                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";
160         }
161
162         int index() const
163         {
164                 return index_;
165         }
166
167         boost::property_tree::wptree info() const
168         {
169                 boost::property_tree::wptree info;
170
171                 auto stage_info  = stage_.info();
172                 auto mixer_info  = mixer_.info();
173                 auto output_info = output_.info();
174
175                 info.add(L"video-mode", format_desc_.name);
176                 info.add_child(L"stage", stage_info.get());
177                 info.add_child(L"mixer", mixer_info.get());
178                 info.add_child(L"output", output_info.get());
179    
180                 return info;                       
181         }
182
183         boost::property_tree::wptree delay_info() const
184         {
185                 boost::property_tree::wptree info;
186
187                 auto stage_info = stage_.delay_info();
188                 auto mixer_info = mixer_.delay_info();
189                 auto output_info = output_.delay_info();
190
191                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
192                 info.add_child(L"layers", stage_info.get());
193                 info.add_child(L"mix-time", mixer_info.get());
194                 info.add_child(L"output", output_info.get());
195
196                 return info;
197         }
198 };
199
200 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))){}
201 video_channel::~video_channel(){}
202 const stage& video_channel::stage() const { return impl_->stage_;} 
203 stage& video_channel::stage() { return impl_->stage_;} 
204 const mixer& video_channel::mixer() const{ return impl_->mixer_;} 
205 mixer& video_channel::mixer() { return impl_->mixer_;} 
206 const output& video_channel::output() const { return impl_->output_;} 
207 output& video_channel::output() { return impl_->output_;} 
208 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;} 
209 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
210 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
211 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
212 boost::property_tree::wptree video_channel::delay_info() const { return impl_->delay_info(); }
213 int video_channel::index() const { return impl_->index(); }
214 monitor::subject& video_channel::monitor_output(){ return *impl_->monitor_subject_; }
215
216 }}