]> git.sesse.net Git - casparcg/blob - shell/server.cpp
3607983: Added support for generating thumbnails
[casparcg] / shell / server.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 \r
23 #include "server.h"\r
24 \r
25 #include <memory>\r
26 \r
27 #include <common/env.h>\r
28 #include <common/exception/exceptions.h>\r
29 #include <common/utility/string.h>\r
30 #include <common/filesystem/polling_filesystem_monitor.h>\r
31 \r
32 #include <core/mixer/gpu/ogl_device.h>\r
33 #include <core/video_channel.h>\r
34 #include <core/producer/stage.h>\r
35 #include <core/consumer/output.h>\r
36 #include <core/thumbnail_generator.h>\r
37 \r
38 #include <modules/bluefish/bluefish.h>\r
39 #include <modules/decklink/decklink.h>\r
40 #include <modules/ffmpeg/ffmpeg.h>\r
41 #include <modules/flash/flash.h>\r
42 #include <modules/oal/oal.h>\r
43 #include <modules/ogl/ogl.h>\r
44 #include <modules/silverlight/silverlight.h>\r
45 #include <modules/image/image.h>\r
46 #include <modules/image/consumer/image_consumer.h>\r
47 \r
48 #include <modules/oal/consumer/oal_consumer.h>\r
49 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
50 #include <modules/decklink/consumer/decklink_consumer.h>\r
51 #include <modules/ogl/consumer/ogl_consumer.h>\r
52 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
53 \r
54 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
55 #include <protocol/cii/CIIProtocolStrategy.h>\r
56 #include <protocol/CLK/CLKProtocolStrategy.h>\r
57 #include <protocol/util/AsyncEventServer.h>\r
58 #include <protocol/util/stateful_protocol_strategy_wrapper.h>\r
59 \r
60 #include <boost/algorithm/string.hpp>\r
61 #include <boost/lexical_cast.hpp>\r
62 #include <boost/foreach.hpp>\r
63 #include <boost/property_tree/ptree.hpp>\r
64 #include <boost/property_tree/xml_parser.hpp>\r
65 \r
66 namespace caspar {\r
67 \r
68 using namespace core;\r
69 using namespace protocol;\r
70 \r
71 struct server::implementation : boost::noncopyable\r
72 {\r
73         safe_ptr<ogl_device>                                            ogl_;\r
74         std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;     \r
75         std::vector<safe_ptr<video_channel>>            channels_;\r
76         std::shared_ptr<thumbnail_generator>            thumbnail_generator_;\r
77 \r
78         implementation()                \r
79                 : ogl_(ogl_device::create())\r
80         {                       \r
81                 ffmpeg::init();\r
82                 CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
83                                                           \r
84                 bluefish::init();         \r
85                 CASPAR_LOG(info) << L"Initialized bluefish module.";\r
86                                                           \r
87                 decklink::init();         \r
88                 CASPAR_LOG(info) << L"Initialized decklink module.";\r
89                                                                                                                   \r
90                 oal::init();              \r
91                 CASPAR_LOG(info) << L"Initialized oal module.";\r
92                                                           \r
93                 ogl::init();              \r
94                 CASPAR_LOG(info) << L"Initialized ogl module.";\r
95 \r
96                 image::init();            \r
97                 CASPAR_LOG(info) << L"Initialized image module.";\r
98 \r
99                 flash::init();            \r
100                 CASPAR_LOG(info) << L"Initialized flash module.";\r
101 \r
102                 setup_channels(env::properties());\r
103                 CASPAR_LOG(info) << L"Initialized channels.";\r
104 \r
105                 setup_thumbnail_generation(env::properties());\r
106 \r
107                 setup_controllers(env::properties());\r
108                 CASPAR_LOG(info) << L"Initialized controllers.";\r
109         }\r
110 \r
111         ~implementation()\r
112         {               \r
113                 ffmpeg::uninit();\r
114 \r
115                 async_servers_.clear();\r
116                 channels_.clear();\r
117         }\r
118                                 \r
119         void setup_channels(const boost::property_tree::wptree& pt)\r
120         {   \r
121                 using boost::property_tree::wptree;\r
122                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
123                 {               \r
124                         auto format_desc = video_format_desc::get(widen(xml_channel.second.get(L"video-mode", L"PAL")));                \r
125                         if(format_desc.format == video_format::invalid)\r
126                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
127                         \r
128                         channels_.push_back(make_safe<video_channel>(channels_.size()+1, format_desc, ogl_));\r
129                         \r
130                         BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))\r
131                         {\r
132                                 try\r
133                                 {\r
134                                         auto name = xml_consumer.first;\r
135                                         if(name == L"screen")\r
136                                                 channels_.back()->output()->add(ogl::create_consumer(xml_consumer.second));                                     \r
137                                         else if(name == L"bluefish")                                    \r
138                                                 channels_.back()->output()->add(bluefish::create_consumer(xml_consumer.second));                                        \r
139                                         else if(name == L"decklink")                                    \r
140                                                 channels_.back()->output()->add(decklink::create_consumer(xml_consumer.second));                                \r
141                                         else if(name == L"file")                                        \r
142                                                 channels_.back()->output()->add(ffmpeg::create_consumer(xml_consumer.second));                                          \r
143                                         else if(name == L"system-audio")\r
144                                                 channels_.back()->output()->add(oal::create_consumer());                \r
145                                         else if(name != L"<xmlcomment>")\r
146                                                 CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
147                                 }\r
148                                 catch(...)\r
149                                 {\r
150                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
151                                 }\r
152                         }                                                       \r
153                 }\r
154 \r
155                 // Dummy diagnostics channel\r
156                 if(env::properties().get(L"configuration.channel-grid", false))\r
157                         channels_.push_back(make_safe<video_channel>(channels_.size()+1, core::video_format_desc::get(core::video_format::x576p2500), ogl_));\r
158         }\r
159                 \r
160         void setup_controllers(const boost::property_tree::wptree& pt)\r
161         {               \r
162                 using boost::property_tree::wptree;\r
163                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
164                 {\r
165                         try\r
166                         {\r
167                                 auto name = xml_controller.first;\r
168                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
169 \r
170                                 if(name == L"tcp")\r
171                                 {                                       \r
172                                         unsigned int port = xml_controller.second.get(L"port", 5250);\r
173                                         auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
174                                         asyncbootstrapper->Start();\r
175                                         async_servers_.push_back(asyncbootstrapper);\r
176                                 }\r
177                                 else\r
178                                         CASPAR_LOG(warning) << "Invalid controller: " << widen(name);   \r
179                         }\r
180                         catch(...)\r
181                         {\r
182                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
183                         }\r
184                 }\r
185         }\r
186 \r
187         void setup_thumbnail_generation(const boost::property_tree::wptree& pt)\r
188         {\r
189                 if (!pt.get(L"configuration.thumbnails.generate-thumbnails", true))\r
190                         return;\r
191 \r
192                 auto scan_interval_millis = pt.get(L"configuration.thumbnails.scan-interval-millis", 5000);\r
193 \r
194                 polling_filesystem_monitor_factory monitor_factory(scan_interval_millis);\r
195                 thumbnail_generator_.reset(new thumbnail_generator(\r
196                                 monitor_factory, \r
197                                 env::media_folder(),\r
198                                 env::thumbnails_folder(),\r
199                                 pt.get(L"configuration.thumbnails.width", 256),\r
200                                 pt.get(L"configuration.thumbnails.height", 144),\r
201                                 core::video_format_desc::get(pt.get(L"configuration.thumbnails.video-mode", L"720p2500")),\r
202                                 ogl_,\r
203                                 pt.get(L"configuration.thumbnails.generate-delay-millis", 2000),\r
204                                 &image::write_cropped_png));\r
205 \r
206                 CASPAR_LOG(info) << L"Initialized thumbnail generator.";\r
207         }\r
208 \r
209         safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
210         {\r
211                 if(boost::iequals(name, L"AMCP"))\r
212                         return make_safe<amcp::AMCPProtocolStrategy>(channels_, thumbnail_generator_);\r
213                 else if(boost::iequals(name, L"CII"))\r
214                         return make_safe<cii::CIIProtocolStrategy>(channels_);\r
215                 else if(boost::iequals(name, L"CLOCK"))\r
216                         //return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
217                         return make_safe<IO::stateful_protocol_strategy_wrapper>([=]\r
218                         {\r
219                                 return std::make_shared<CLK::CLKProtocolStrategy>(channels_);\r
220                         });\r
221                 \r
222                 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(narrow(name)) << msg_info("Invalid protocol"));\r
223         }\r
224 };\r
225 \r
226 server::server() : impl_(new implementation()){}\r
227 \r
228 const std::vector<safe_ptr<video_channel>> server::get_channels() const\r
229 {\r
230         return impl_->channels_;\r
231 }\r
232 \r
233 std::shared_ptr<thumbnail_generator> server::get_thumbnail_generator() const\r
234 {\r
235         return impl_->thumbnail_generator_;\r
236 }\r
237 \r
238 }