]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
57753c07764b2db41557e8c10caf54bbdb81cf9b
[casparcg] / core / video_channel.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "StdAfx.h"\r
23 \r
24 #include "video_channel.h"\r
25 \r
26 #include "video_format.h"\r
27 \r
28 #include "producer/stage.h"\r
29 #include "mixer/mixer.h"\r
30 #include "consumer/output.h"\r
31 #include "frame/frame.h"\r
32 #include "frame/draw_frame.h"\r
33 #include "frame/frame_factory.h"\r
34 \r
35 #include <common/diagnostics/graph.h>\r
36 #include <common/env.h>\r
37 #include <common/lock.h>\r
38 #include <common/executor.h>\r
39 \r
40 #include <core/mixer/image/image_mixer.h>\r
41 \r
42 #include <tbb/spin_mutex.h>\r
43 \r
44 #include <boost/property_tree/ptree.hpp>\r
45 \r
46 #include <string>\r
47 \r
48 namespace caspar { namespace core {\r
49 \r
50 struct video_channel::impl sealed\r
51 {\r
52         monitor::basic_subject                                                  event_subject_;\r
53 \r
54         const int                                                                               index_;\r
55 \r
56         mutable tbb::spin_mutex                                                 format_desc_mutex_;\r
57         core::video_format_desc                                                 format_desc_;\r
58         \r
59         const spl::shared_ptr<diagnostics::graph>               graph_;\r
60 \r
61         caspar::core::output                                                    output_;\r
62         spl::shared_ptr<image_mixer>                                    image_mixer_;\r
63         caspar::core::mixer                                                             mixer_;\r
64         caspar::core::stage                                                             stage_; \r
65 \r
66         executor                                                                                executor_;\r
67 public:\r
68         impl(int index, const core::video_format_desc& format_desc, std::unique_ptr<image_mixer> image_mixer)  \r
69                 : event_subject_(monitor::path() % "channel" % index)\r
70                 , index_(index)\r
71                 , format_desc_(format_desc)\r
72                 , output_(graph_, format_desc, index)\r
73                 , image_mixer_(std::move(image_mixer))\r
74                 , mixer_(graph_, image_mixer_)\r
75                 , stage_(graph_)\r
76                 , executor_(L"video_channel")\r
77         {\r
78                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
79                 graph_->set_text(print());\r
80                 diagnostics::register_graph(graph_);\r
81                 \r
82                 output_.subscribe(event_subject_);\r
83                 stage_.subscribe(event_subject_);\r
84 \r
85                 executor_.begin_invoke([=]{tick();});\r
86 \r
87                 CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
88         }\r
89                                                         \r
90         core::video_format_desc video_format_desc() const\r
91         {\r
92                 return lock(format_desc_mutex_, [&]\r
93                 {\r
94                         return format_desc_;\r
95                 });\r
96         }\r
97                 \r
98         void video_format_desc(const core::video_format_desc& format_desc)\r
99         {\r
100                 lock(format_desc_mutex_, [&]\r
101                 {\r
102                         format_desc_ = format_desc;\r
103                         stage_.clear();\r
104                 });\r
105         }\r
106 \r
107         void tick()\r
108         {\r
109                 try\r
110                 {\r
111                         boost::timer t;\r
112 \r
113                         auto format_desc = video_format_desc();\r
114                         \r
115                         boost::timer frame_timer;\r
116 \r
117                         // Produce\r
118                         \r
119                         auto stage_frames = stage_(format_desc);\r
120 \r
121                         if(t.elapsed() > 1.0/format_desc.fps*0.9)\r
122                                 CASPAR_LOG(trace) << print() << L" Stage is slowing down channel: " << t.elapsed();\r
123 \r
124                         t.restart();\r
125                         // Mix\r
126                         \r
127                         auto mixed_frame  = mixer_(std::move(stage_frames), format_desc);\r
128                         \r
129                         if(t.elapsed() > 1.0/format_desc.fps*0.9)\r
130                                 CASPAR_LOG(trace) << print() << L"Mixer is slowing down channel: " << t.elapsed();\r
131 \r
132                         t.restart();\r
133                         // Consume\r
134                                                 \r
135                         output_(std::move(mixed_frame), format_desc);\r
136                 \r
137                         t.restart();\r
138 \r
139                         graph_->set_value("tick-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
140 \r
141                         event_subject_  << monitor::event("profiler/time")      % frame_timer.elapsed() % (1.0/format_desc_.fps)\r
142                                                         << monitor::event("format")                     % format_desc.name;\r
143                         \r
144                         if(t.elapsed() > 0.001)\r
145                                 CASPAR_LOG(trace) << L"4## " << t.elapsed();\r
146 \r
147                         t.restart();\r
148                 }\r
149                 catch(...)\r
150                 {\r
151                         CASPAR_LOG_CURRENT_EXCEPTION();\r
152                 }\r
153 \r
154                 executor_.begin_invoke([=]{tick();});\r
155         }\r
156                         \r
157         std::wstring print() const\r
158         {\r
159                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  video_format_desc().name + L"]";\r
160         }\r
161 \r
162         boost::property_tree::wptree info() const\r
163         {\r
164                 boost::property_tree::wptree info;\r
165 \r
166                 auto stage_info  = stage_.info();\r
167                 auto mixer_info  = mixer_.info();\r
168                 auto output_info = output_.info();\r
169 \r
170                 info.add(L"video-mode", format_desc_.name);\r
171                 info.add_child(L"stage", stage_info.get());\r
172                 info.add_child(L"mixer", mixer_info.get());\r
173                 info.add_child(L"output", output_info.get());\r
174    \r
175                 return info;                       \r
176         }\r
177 };\r
178 \r
179 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))){}\r
180 video_channel::~video_channel(){}\r
181 const stage& video_channel::stage() const { return impl_->stage_;} \r
182 stage& video_channel::stage() { return impl_->stage_;} \r
183 const mixer& video_channel::mixer() const{ return impl_->mixer_;} \r
184 mixer& video_channel::mixer() { return impl_->mixer_;} \r
185 const output& video_channel::output() const { return impl_->output_;} \r
186 output& video_channel::output() { return impl_->output_;} \r
187 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_->image_mixer_;} \r
188 core::video_format_desc video_channel::video_format_desc() const{return impl_->video_format_desc();}\r
189 void core::video_channel::video_format_desc(const core::video_format_desc& format_desc){impl_->video_format_desc(format_desc);}\r
190 boost::property_tree::wptree video_channel::info() const{return impl_->info();}         \r
191 void video_channel::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}\r
192 void video_channel::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}\r
193 \r
194 }}