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