]> git.sesse.net Git - casparcg/blob - core/video_channel.cpp
2.1.0: -ogl project: Renamed to screen. -extracted gpu specific stuff into accelerato...
[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/data_frame.h"\r
32 #include "frame/frame_factory.h"\r
33 \r
34 #include <common/diagnostics/graph.h>\r
35 #include <common/env.h>\r
36 #include <common/concurrency/lock.h>\r
37 #include <common/concurrency/executor.h>\r
38 \r
39 #include <tbb/spin_mutex.h>\r
40 \r
41 #include <boost/property_tree/ptree.hpp>\r
42 \r
43 #include <string>\r
44 \r
45 namespace caspar { namespace core {\r
46 \r
47 struct video_channel::impl sealed : public frame_factory\r
48 {\r
49         reactive::basic_subject<spl::shared_ptr<const data_frame>> frame_subject_;\r
50 \r
51         const int                                                                               index_;\r
52 \r
53         mutable tbb::spin_mutex                                                 format_desc_mutex_;\r
54         video_format_desc                                                               format_desc_;\r
55         \r
56         const spl::shared_ptr<diagnostics::graph>               graph_;\r
57 \r
58         const spl::shared_ptr<caspar::core::output>             output_;\r
59         const spl::shared_ptr<caspar::core::mixer>              mixer_;\r
60         const spl::shared_ptr<caspar::core::stage>              stage_; \r
61 \r
62         boost::timer                                                                    tick_timer_;\r
63         boost::timer                                                                    produce_timer_;\r
64         boost::timer                                                                    mix_timer_;\r
65         boost::timer                                                                    consume_timer_;\r
66 \r
67         executor                                                                                executor_;\r
68 public:\r
69         impl(int index, const video_format_desc& format_desc, spl::shared_ptr<image_mixer> image_mixer)  \r
70                 :  index_(index)\r
71                 , format_desc_(format_desc)\r
72                 , output_(new caspar::core::output(format_desc, index))\r
73                 , mixer_(new caspar::core::mixer(std::move(image_mixer)))\r
74                 , stage_(new caspar::core::stage())     \r
75                 , executor_(L"video_channel")\r
76         {\r
77                 //graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));\r
78                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
79                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
80                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));\r
81                 graph_->set_text(print());\r
82                 diagnostics::register_graph(graph_);\r
83 \r
84                 executor_.begin_invoke([=]{tick();});\r
85 \r
86                 CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
87         }\r
88         \r
89         // frame_factory\r
90                                                 \r
91         virtual spl::shared_ptr<write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc) override\r
92         {               \r
93                 return mixer_->create_frame(tag, desc);\r
94         }\r
95         \r
96         virtual core::video_format_desc get_video_format_desc() const override\r
97         {\r
98                 return lock(format_desc_mutex_, [&]\r
99                 {\r
100                         return format_desc_;\r
101                 });\r
102         }\r
103         \r
104         // video_channel\r
105 \r
106         void tick()\r
107         {\r
108                 tick_timer_.restart();\r
109 \r
110                 // Produce\r
111 \r
112                 produce_timer_.restart();\r
113 \r
114                 auto stage_frames = (*stage_)(format_desc_);\r
115                 \r
116                 graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
117 \r
118                 // Mix\r
119 \r
120                 //mix_timer_.restart();\r
121 \r
122                 auto mixed_frame  = (*mixer_)(std::move(stage_frames), format_desc_);\r
123                 \r
124                 //graph_->set_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);\r
125 \r
126                 // Consume\r
127 \r
128                 consume_timer_.restart();\r
129 \r
130                 frame_subject_.on_next(mixed_frame);\r
131 \r
132                 graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
133 \r
134                 (*output_)(std::move(mixed_frame), format_desc_);\r
135                 \r
136                 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
137 \r
138                 executor_.begin_invoke([=]{tick();});\r
139         }\r
140 \r
141         void set_video_format_desc(const video_format_desc& format_desc)\r
142         {\r
143                 lock(format_desc_mutex_, [&]\r
144                 {\r
145                         format_desc_ = format_desc;\r
146                 });\r
147         }\r
148                 \r
149         std::wstring print() const\r
150         {\r
151                 return L"video_channel[" + boost::lexical_cast<std::wstring>(index_) + L"|" +  format_desc_.name + L"]";\r
152         }\r
153 \r
154         boost::property_tree::wptree info() const\r
155         {\r
156                 boost::property_tree::wptree info;\r
157 \r
158                 auto stage_info  = stage_->info();\r
159                 auto mixer_info  = mixer_->info();\r
160                 auto output_info = output_->info();\r
161 \r
162                 info.add(L"video-mode", format_desc_.name);\r
163                 info.add_child(L"stage", stage_info.get());\r
164                 info.add_child(L"mixer", mixer_info.get());\r
165                 info.add_child(L"output", output_info.get());\r
166    \r
167                 return info;                       \r
168         }\r
169 };\r
170 \r
171 video_channel::video_channel(int index, const video_format_desc& format_desc, spl::shared_ptr<image_mixer> image_mixer) : impl_(new impl(index, format_desc, image_mixer)){}\r
172 spl::shared_ptr<stage> video_channel::stage() { return impl_->stage_;} \r
173 spl::shared_ptr<mixer> video_channel::mixer() { return impl_->mixer_;} \r
174 spl::shared_ptr<frame_factory> video_channel::frame_factory() { return impl_;} \r
175 spl::shared_ptr<output> video_channel::output() { return impl_->output_;} \r
176 video_format_desc video_channel::get_video_format_desc() const{return impl_->format_desc_;}\r
177 void video_channel::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
178 boost::property_tree::wptree video_channel::info() const{return impl_->info();}\r
179 void video_channel::subscribe(const observer_ptr& o) {impl_->frame_subject_.subscribe(o);}\r
180 void video_channel::unsubscribe(const observer_ptr& o) {impl_->frame_subject_.unsubscribe(o);}\r
181 \r
182 }}