]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
[README] Updated for 2.1.0 Beta 1
[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 #include <common/future.h>
42
43 #include <core/mixer/image/image_mixer.h>
44 #include <core/diagnostics/call_context.h>
45
46 #include <tbb/spin_mutex.h>
47
48 #include <boost/property_tree/ptree.hpp>
49 #include <boost/lexical_cast.hpp>
50
51 #include <string>
52 #include <unordered_map>
53
54 namespace caspar { namespace core {
55
56 struct video_channel::impl final
57 {
58         spl::shared_ptr<monitor::subject>                                       monitor_subject_;
59
60         const int                                                                                       index_;
61
62         mutable tbb::spin_mutex                                                         format_desc_mutex_;
63         core::video_format_desc                                                         format_desc_;
64         mutable tbb::spin_mutex                                                         channel_layout_mutex_;
65         core::audio_channel_layout                                                      channel_layout_;
66
67         const spl::shared_ptr<caspar::diagnostics::graph>       graph_                                  = [](int index)
68                                                                                                                                                                   {
69                                                                                                                                                                           core::diagnostics::scoped_call_context save;
70                                                                                                                                                                           core::diagnostics::call_context::for_thread().video_channel = index;
71                                                                                                                                                                           return spl::make_shared<caspar::diagnostics::graph>();
72                                                                                                                                                                   }(index_);
73
74         caspar::core::output                                                            output_;
75         std::future<void>                                                                       output_ready_for_frame_ = make_ready_future();
76         spl::shared_ptr<image_mixer>                                            image_mixer_;
77         caspar::core::mixer                                                                     mixer_;
78         caspar::core::stage                                                                     stage_;
79
80         mutable tbb::spin_mutex                                                         tick_listeners_mutex_;
81         int64_t                                                                                         last_tick_listener_id   = 0;
82         std::unordered_map<int64_t, std::function<void ()>>     tick_listeners_;
83
84         executor                                                                                        executor_                               { L"video_channel " + boost::lexical_cast<std::wstring>(index_) };
85 public:
86         impl(
87                         int index,
88                         const core::video_format_desc& format_desc,
89                         const core::audio_channel_layout& channel_layout,
90                         std::unique_ptr<image_mixer> image_mixer)
91                 : monitor_subject_(spl::make_shared<monitor::subject>(
92                                 "/channel/" + boost::lexical_cast<std::string>(index)))
93                 , index_(index)
94                 , format_desc_(format_desc)
95                 , channel_layout_(channel_layout)
96                 , output_(graph_, format_desc, channel_layout, index)
97                 , image_mixer_(std::move(image_mixer))
98                 , mixer_(index, graph_, image_mixer_)
99                 , stage_(index, graph_)
100         {
101                 graph_->set_color("tick-time", caspar::diagnostics::color(0.0f, 0.6f, 0.9f));
102                 graph_->set_text(print());
103                 caspar::diagnostics::register_graph(graph_);
104
105                 output_.monitor_output().attach_parent(monitor_subject_);
106                 mixer_.monitor_output().attach_parent(monitor_subject_);
107                 stage_.monitor_output().attach_parent(monitor_subject_);
108
109                 executor_.begin_invoke([=]{tick();});
110
111                 CASPAR_LOG(info) << print() << " Successfully Initialized.";
112         }
113
114         ~impl()
115         {
116                 CASPAR_LOG(info) << print() << " Uninitializing.";
117         }
118
119         core::video_format_desc video_format_desc() const
120         {
121                 return lock(format_desc_mutex_, [&]
122                 {
123                         return format_desc_;
124                 });
125         }
126
127         void video_format_desc(const core::video_format_desc& format_desc)
128         {
129                 lock(format_desc_mutex_, [&]
130                 {
131                         format_desc_ = format_desc;
132                         stage_.clear();
133                 });
134         }
135
136         core::audio_channel_layout audio_channel_layout() const
137         {
138                 return lock(channel_layout_mutex_, [&]
139                 {
140                         return channel_layout_;
141                 });
142         }
143
144         void audio_channel_layout(const core::audio_channel_layout& channel_layout)
145         {
146                 lock(channel_layout_mutex_, [&]
147                 {
148                         channel_layout_ = channel_layout;
149                         stage_.clear();
150                 });
151         }
152
153         void invoke_tick_listeners()
154         {
155                 auto listeners = lock(tick_listeners_mutex_, [=] { return tick_listeners_; });
156
157                 for (auto listener : listeners)
158                 {
159                         try
160                         {
161                                 listener.second();
162                         }
163                         catch (...)
164                         {
165                                 CASPAR_LOG_CURRENT_EXCEPTION();
166                         }
167                 }
168         }
169
170         void tick()
171         {
172                 try
173                 {
174                         invoke_tick_listeners();
175
176                         auto format_desc        = video_format_desc();
177                         auto channel_layout = audio_channel_layout();
178
179                         caspar::timer frame_timer;
180
181                         // Produce
182
183                         auto stage_frames = stage_(format_desc);
184
185                         // Mix
186
187                         auto mixed_frame  = mixer_(std::move(stage_frames), format_desc, channel_layout);
188
189                         // Consume
190
191                         output_ready_for_frame_ = output_(std::move(mixed_frame), format_desc, channel_layout);
192                         output_ready_for_frame_.get();
193
194                         auto frame_time = frame_timer.elapsed()*format_desc.fps*0.5;
195                         graph_->set_value("tick-time", frame_time);
196
197                         *monitor_subject_       << monitor::message("/profiler/time")   % frame_timer.elapsed() % (1.0/ video_format_desc().fps)
198                                                                 << monitor::message("/format")                  % format_desc.name;
199                 }
200                 catch(...)
201                 {
202                         CASPAR_LOG_CURRENT_EXCEPTION();
203                 }
204
205                 if (executor_.is_running())
206                         executor_.begin_invoke([=]{tick();});
207         }
208
209         std::wstring print() const
210         {
211                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";
212         }
213
214         int index() const
215         {
216                 return index_;
217         }
218
219         boost::property_tree::wptree info() const
220         {
221                 boost::property_tree::wptree info;
222
223                 auto stage_info  = stage_.info();
224                 auto mixer_info  = mixer_.info();
225                 auto output_info = output_.info();
226
227                 info.add(L"video-mode", video_format_desc().name);
228                 info.add(L"audio-channel-layout", audio_channel_layout().print());
229                 info.add_child(L"stage", stage_info.get());
230                 info.add_child(L"mixer", mixer_info.get());
231                 info.add_child(L"output", output_info.get());
232
233                 return info;
234         }
235
236         boost::property_tree::wptree delay_info() const
237         {
238                 boost::property_tree::wptree info;
239
240                 auto stage_info = stage_.delay_info();
241                 auto mixer_info = mixer_.delay_info();
242                 auto output_info = output_.delay_info();
243
244                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
245                 info.add_child(L"layers", stage_info.get());
246                 info.add_child(L"mix-time", mixer_info.get());
247                 info.add_child(L"output", output_info.get());
248
249                 return info;
250         }
251
252         std::shared_ptr<void> add_tick_listener(std::function<void()> listener)
253         {
254                 return lock(tick_listeners_mutex_, [&]
255                 {
256                         auto tick_listener_id = last_tick_listener_id++;
257                         tick_listeners_.insert(std::make_pair(tick_listener_id, listener));
258
259                         return std::shared_ptr<void>(nullptr, [=](void*)
260                         {
261                                 lock(tick_listeners_mutex_, [&]
262                                 {
263                                         tick_listeners_.erase(tick_listener_id);
264                                 });
265                         });
266                 });
267         }
268 };
269
270 video_channel::video_channel(
271                 int index,
272                 const core::video_format_desc& format_desc,
273                 const core::audio_channel_layout& channel_layout,
274                 std::unique_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, channel_layout, std::move(image_mixer))){}
275 video_channel::~video_channel(){}
276 const stage& video_channel::stage() const { return impl_->stage_;}
277 stage& video_channel::stage() { return impl_->stage_;}
278 const mixer& video_channel::mixer() const{ return impl_->mixer_;}
279 mixer& video_channel::mixer() { return impl_->mixer_;}
280 const output& video_channel::output() const { return impl_->output_;}
281 output& video_channel::output() { return impl_->output_;}
282 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;}
283 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}
284 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}
285 core::audio_channel_layout video_channel::audio_channel_layout() const { return impl_->audio_channel_layout(); }
286 void core::video_channel::audio_channel_layout(const core::audio_channel_layout& channel_layout) { impl_->audio_channel_layout(channel_layout); }
287 boost::property_tree::wptree video_channel::info() const{return impl_->info();}
288 boost::property_tree::wptree video_channel::delay_info() const { return impl_->delay_info(); }
289 int video_channel::index() const { return impl_->index(); }
290 monitor::subject& video_channel::monitor_output(){ return *impl_->monitor_subject_; }
291 std::shared_ptr<void> video_channel::add_tick_listener(std::function<void()> listener) { return impl_->add_tick_listener(std::move(listener)); }
292
293 }}