]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
2.0.2: MAYOR CHANGES => needs testing
[casparcg] / core / video_channel.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include "StdAfx.h"\r
22 \r
23 #include "video_channel.h"\r
24 \r
25 #include "video_channel_context.h"\r
26 #include "video_format.h"\r
27 \r
28 #include "consumer/output.h"\r
29 #include "mixer/mixer.h"\r
30 #include "producer/stage.h"\r
31 \r
32 #include <common/concurrency/executor.h>\r
33 #include <common/diagnostics/graph.h>\r
34 \r
35 #include "mixer/gpu/ogl_device.h"\r
36 \r
37 #include <boost/timer.hpp>\r
38 \r
39 #ifdef _MSC_VER\r
40 #pragma warning(disable : 4355)\r
41 #endif\r
42 \r
43 namespace caspar { namespace core {\r
44 \r
45 struct video_channel::implementation : boost::noncopyable\r
46 {\r
47         video_channel_context                   context_;\r
48 \r
49         safe_ptr<caspar::core::output>                  output_;\r
50         std::shared_ptr<caspar::core::mixer>    mixer_;\r
51         safe_ptr<caspar::core::stage>                   stage_;\r
52 \r
53         safe_ptr<diagnostics::graph>    diag_;\r
54         boost::timer                                    frame_timer_;\r
55         boost::timer                                    tick_timer_;\r
56         boost::timer                                    output_timer_;\r
57         \r
58 public:\r
59         implementation(int index, const video_format_desc& format_desc, ogl_device& ogl)  \r
60                 : context_(index, ogl, format_desc)\r
61                 , output_(new caspar::core::output(context_, [this]{restart();}))\r
62                 , mixer_(new caspar::core::mixer(context_))\r
63                 , stage_(new caspar::core::stage(context_))     \r
64         {\r
65                 diag_->add_guide("produce-time", 0.5f); \r
66                 diag_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
67                 diag_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));    \r
68                 diag_->set_color("output-time", diagnostics::color(1.0f, 0.5f, 0.0f));\r
69                 diag_->set_color("mix-time", diagnostics::color(1.0f, 1.0f, 0.9f));\r
70                 diag_->set_text(print());\r
71                 diagnostics::register_graph(diag_);\r
72 \r
73                 CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
74                 context_.execution().begin_invoke([this]{tick();});\r
75         }\r
76 \r
77         ~implementation()\r
78         {\r
79                 // Stop context before destroying devices.\r
80                 context_.execution().stop();\r
81                 context_.execution().join();\r
82         }\r
83 \r
84         void tick()\r
85         {\r
86                 try\r
87                 {\r
88                         // Produce\r
89 \r
90                         frame_timer_.restart();\r
91 \r
92                         auto simple_frames = stage_->execute();\r
93 \r
94                         diag_->update_value("produce-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
95                 \r
96                         // Mix\r
97 \r
98                         frame_timer_.restart();\r
99 \r
100                         auto finished_frame = mixer_->execute(simple_frames);\r
101                 \r
102                         diag_->update_value("mix-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
103                 \r
104                         // Consume\r
105                 \r
106                         output_timer_.restart();\r
107 \r
108                         output_->execute(finished_frame);\r
109                 \r
110                         diag_->update_value("output-time", frame_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
111 \r
112                 \r
113                         diag_->update_value("tick-time", tick_timer_.elapsed()*context_.get_format_desc().fps*0.5);\r
114                         tick_timer_.restart();\r
115                 }\r
116                 catch(...)\r
117                 {\r
118                         CASPAR_LOG_CURRENT_EXCEPTION();\r
119                         CASPAR_LOG(error) << context_.print() << L" Unexpected exception. Clearing stage and freeing memory";\r
120                         restart();\r
121                 }\r
122 \r
123                 context_.execution().begin_invoke([this]{tick();});\r
124         }\r
125 \r
126         void restart()\r
127         {\r
128                 stage_->clear();\r
129                 context_.ogl().gc().wait();\r
130 \r
131                 mixer_ = nullptr;\r
132                 mixer_.reset(new caspar::core::mixer(context_));\r
133         }\r
134                 \r
135         std::wstring print() const\r
136         {\r
137                 return context_.print();\r
138         }\r
139 \r
140         void set_video_format_desc(const video_format_desc& format_desc)\r
141         {\r
142                 context_.execution().begin_invoke([=]\r
143                 {\r
144                         stage_->clear();\r
145                         context_.ogl().gc().wait();\r
146                         context_.set_format_desc(format_desc);\r
147                 });\r
148         }\r
149 };\r
150 \r
151 video_channel::video_channel(int index, const video_format_desc& format_desc, ogl_device& ogl) : impl_(new implementation(index, format_desc, ogl)){}\r
152 video_channel::video_channel(video_channel&& other) : impl_(std::move(other.impl_)){}\r
153 safe_ptr<stage> video_channel::stage() { return impl_->stage_;} \r
154 safe_ptr<mixer> video_channel::mixer() { return make_safe_ptr(impl_->mixer_);} \r
155 safe_ptr<output> video_channel::output() { return impl_->output_;} \r
156 video_format_desc video_channel::get_video_format_desc() const{return impl_->context_.get_format_desc();}\r
157 void video_channel::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
158 std::wstring video_channel::print() const { return impl_->print();}\r
159 video_channel_context& video_channel::context(){return impl_->context_;}\r
160 \r
161 }}