]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
* Added font and font size to info() in text_producer
[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 #include "frame/audio_channel_layout.h"
35
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>
41
42 #include <core/mixer/image/image_mixer.h>
43 #include <core/diagnostics/call_context.h>
44
45 #include <tbb/spin_mutex.h>
46
47 #include <boost/property_tree/ptree.hpp>
48 #include <boost/lexical_cast.hpp>
49
50 #include <string>
51
52 namespace caspar { namespace core {
53
54 struct video_channel::impl final
55 {
56         spl::shared_ptr<monitor::subject>                                       monitor_subject_;
57
58         const int                                                                                       index_;
59
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_;
64
65         const spl::shared_ptr<caspar::diagnostics::graph>       graph_                          = [](int index)
66                                                                                                                                                           {
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>();
70                                                                                                                                                           }(index_);
71
72         caspar::core::output                                                            output_;
73         spl::shared_ptr<image_mixer>                                            image_mixer_;
74         caspar::core::mixer                                                                     mixer_;
75         caspar::core::stage                                                                     stage_; 
76
77         executor                                                                                        executor_                       { L"video_channel " + boost::lexical_cast<std::wstring>(index_) };
78 public:
79         impl(
80                         int 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)))
86                 , index_(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_)
93         {
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_);
97
98                 output_.monitor_output().attach_parent(monitor_subject_);
99                 mixer_.monitor_output().attach_parent(monitor_subject_);
100                 stage_.monitor_output().attach_parent(monitor_subject_);
101
102                 executor_.begin_invoke([=]{tick();});
103
104                 CASPAR_LOG(info) << print() << " Successfully Initialized.";
105         }
106
107         ~impl()
108         {
109                 CASPAR_LOG(info) << print() << " Uninitializing.";
110         }
111
112         core::video_format_desc video_format_desc() const
113         {
114                 return lock(format_desc_mutex_, [&]
115                 {
116                         return format_desc_;
117                 });
118         }
119
120         void video_format_desc(const core::video_format_desc& format_desc)
121         {
122                 lock(format_desc_mutex_, [&]
123                 {
124                         format_desc_ = format_desc;
125                         stage_.clear();
126                 });
127         }
128
129         core::audio_channel_layout audio_channel_layout() const
130         {
131                 return lock(channel_layout_mutex_, [&]
132                 {
133                         return channel_layout_;
134                 });
135         }
136
137         void audio_channel_layout(const core::audio_channel_layout& channel_layout)
138         {
139                 lock(channel_layout_mutex_, [&]
140                 {
141                         channel_layout_ = channel_layout;
142                         stage_.clear();
143                 });
144         }
145
146         void tick()
147         {
148                 try
149                 {
150
151                         auto format_desc        = video_format_desc();
152                         auto channel_layout = audio_channel_layout();
153                         
154                         caspar::timer frame_timer;
155
156                         // Produce
157                         
158                         auto stage_frames = stage_(format_desc);
159                         
160                         // Mix
161                         
162                         auto mixed_frame  = mixer_(std::move(stage_frames), format_desc, channel_layout);
163                         
164                         // Consume
165                                                 
166                         output_(std::move(mixed_frame), format_desc, channel_layout);
167                 
168                         auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
169                         graph_->set_value("tick-time", frame_time);
170
171                         *monitor_subject_       << monitor::message("/profiler/time")   % frame_timer.elapsed() % (1.0/format_desc_.fps)
172                                                                 << monitor::message("/format")                  % format_desc.name;
173                 }
174                 catch(...)
175                 {
176                         CASPAR_LOG_CURRENT_EXCEPTION();
177                 }
178
179                 if (executor_.is_running())
180                         executor_.begin_invoke([=]{tick();});
181         }
182                         
183         std::wstring print() const
184         {
185                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";
186         }
187
188         int index() const
189         {
190                 return index_;
191         }
192
193         boost::property_tree::wptree info() const
194         {
195                 boost::property_tree::wptree info;
196
197                 auto stage_info  = stage_.info();
198                 auto mixer_info  = mixer_.info();
199                 auto output_info = output_.info();
200
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());
206    
207                 return info;                       
208         }
209
210         boost::property_tree::wptree delay_info() const
211         {
212                 boost::property_tree::wptree info;
213
214                 auto stage_info = stage_.delay_info();
215                 auto mixer_info = mixer_.delay_info();
216                 auto output_info = output_.delay_info();
217
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());
222
223                 return info;
224         }
225 };
226
227 video_channel::video_channel(
228                 int index,
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_; }
248
249 }}