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