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