]> git.sesse.net Git - casparcg/blob - shell/server.cpp
x64: Conversion complete.
[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 #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/video_format.h>\r
30 #include <core/producer/stage.h>\r
31 #include <core/consumer/output.h>\r
32 \r
33 #include <modules/bluefish/bluefish.h>\r
34 #include <modules/decklink/decklink.h>\r
35 #include <modules/ffmpeg/ffmpeg.h>\r
36 #include <modules/flash/flash.h>\r
37 #include <modules/oal/oal.h>\r
38 #include <modules/ogl/ogl.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                 image::uninit();\r
103                 ffmpeg::uninit();\r
104 \r
105                 async_servers_.clear();\r
106                 channels_.clear();\r
107         }\r
108                                 \r
109         void setup_channels(const boost::property_tree::wptree& pt)\r
110         {   \r
111                 using boost::property_tree::wptree;\r
112                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
113                 {               \r
114                         auto format_desc = video_format_desc::get(xml_channel.second.get(L"video-mode", L"PAL"));               \r
115                         if(format_desc.format == video_format::invalid)\r
116                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
117                         \r
118                         channels_.push_back(make_safe<video_channel>(static_cast<int>(channels_.size()+1), format_desc, ogl_));\r
119                         \r
120                         BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))\r
121                         {\r
122                                 try\r
123                                 {\r
124                                         auto name = xml_consumer.first;\r
125                                         if(name == L"screen")\r
126                                                 channels_.back()->output()->add(ogl::create_consumer(xml_consumer.second));                                     \r
127                                         if(name == L"bluefish")                                 \r
128                                                 channels_.back()->output()->add(bluefish::create_consumer(xml_consumer.second));                                        \r
129                                         else if(name == L"decklink")                                    \r
130                                                 channels_.back()->output()->add(decklink::create_consumer(xml_consumer.second));                                \r
131                                         else if(name == L"file")                                        \r
132                                                 channels_.back()->output()->add(ffmpeg::create_consumer(xml_consumer.second));                                          \r
133                                         else if(name == L"system-audio")\r
134                                                 channels_.back()->output()->add(oal::create_consumer());                \r
135                                         else if(name != L"<xmlcomment>")\r
136                                                 CASPAR_LOG(warning) << "Invalid consumer: " << name;    \r
137                                 }\r
138                                 catch(...)\r
139                                 {\r
140                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
141                                 }\r
142                         }                                                       \r
143                 }\r
144         }\r
145                 \r
146         void setup_controllers(const boost::property_tree::wptree& pt)\r
147         {               \r
148                 using boost::property_tree::wptree;\r
149                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
150                 {\r
151                         try\r
152                         {\r
153                                 auto name = xml_controller.first;\r
154                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
155 \r
156                                 if(name == L"tcp")\r
157                                 {                                       \r
158                                         unsigned int port = xml_controller.second.get(L"port", 5250);\r
159                                         auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
160                                         asyncbootstrapper->Start();\r
161                                         async_servers_.push_back(asyncbootstrapper);\r
162                                 }\r
163                                 else\r
164                                         CASPAR_LOG(warning) << "Invalid controller: " << name;  \r
165                         }\r
166                         catch(...)\r
167                         {\r
168                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
169                         }\r
170                 }\r
171         }\r
172 \r
173         safe_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
174         {\r
175                 if(boost::iequals(name, L"AMCP"))\r
176                         return make_safe<amcp::AMCPProtocolStrategy>(channels_);\r
177                 else if(boost::iequals(name, L"CII"))\r
178                         return make_safe<cii::CIIProtocolStrategy>(channels_);\r
179                 else if(boost::iequals(name, L"CLOCK"))\r
180                         return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
181                 \r
182                 BOOST_THROW_EXCEPTION(caspar_exception() << warg_name_info(L"name") << warg_value_info(name) << wmsg_info(L"Invalid protocol"));\r
183         }\r
184 };\r
185 \r
186 server::server() : impl_(new implementation()){}\r
187 \r
188 const std::vector<safe_ptr<video_channel>> server::get_channels() const\r
189 {\r
190         return impl_->channels_;\r
191 }\r
192 \r
193 }