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