]> git.sesse.net Git - casparcg/blob - shell/server.cpp
2.0.0.2: Added only_key support to all consumers.
[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/channel.h>\r
28 \r
29 #include <modules/bluefish/bluefish.h>\r
30 #include <modules/decklink/decklink.h>\r
31 #include <modules/ffmpeg/ffmpeg.h>\r
32 #include <modules/flash/flash.h>\r
33 #include <modules/oal/oal.h>\r
34 #include <modules/ogl/ogl.h>\r
35 #include <modules/silverlight/silverlight.h>\r
36 #include <modules/image/image.h>\r
37 \r
38 #include <modules/oal/consumer/oal_consumer.h>\r
39 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
40 #include <modules/decklink/consumer/decklink_consumer.h>\r
41 #include <modules/ogl/consumer/ogl_consumer.h>\r
42 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
43 \r
44 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
45 #include <protocol/cii/CIIProtocolStrategy.h>\r
46 #include <protocol/CLK/CLKProtocolStrategy.h>\r
47 #include <protocol/util/AsyncEventServer.h>\r
48 \r
49 #include <boost/algorithm/string.hpp>\r
50 #include <boost/lexical_cast.hpp>\r
51 #include <boost/filesystem.hpp>\r
52 #include <boost/foreach.hpp>\r
53 #include <boost/property_tree/ptree.hpp>\r
54 #include <boost/property_tree/xml_parser.hpp>\r
55 \r
56 namespace caspar {\r
57 \r
58 using namespace core;\r
59 using namespace protocol;\r
60 \r
61 struct server::implementation : boost::noncopyable\r
62 {\r
63         std::vector<safe_ptr<IO::AsyncEventServer>> async_servers_;     \r
64         std::vector<safe_ptr<channel>> channels_;\r
65 \r
66         implementation()                                                                                                \r
67         {                       \r
68                 init_ffmpeg();\r
69                 init_bluefish();\r
70                 init_decklink();\r
71                 init_flash();\r
72                 init_oal();\r
73                 init_ogl();\r
74                 //init_silverlight();\r
75                 init_image();\r
76 \r
77                 setup_channels(env::properties());\r
78                 setup_controllers(env::properties());\r
79         }\r
80 \r
81         ~implementation()\r
82         {                               \r
83                 async_servers_.clear();\r
84                 channels_.clear();\r
85         }\r
86                                 \r
87         void setup_channels(const boost::property_tree::ptree& pt)\r
88         {   \r
89                 using boost::property_tree::ptree;\r
90                 BOOST_FOREACH(auto& xml_channel, pt.get_child("configuration.channels"))\r
91                 {               \r
92                         auto format_desc = video_format_desc::get(widen(xml_channel.second.get("videomode", "PAL")));           \r
93                         if(format_desc.format == video_format::invalid)\r
94                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
95                         \r
96                         channels_.push_back(channel(channels_.size(), format_desc));\r
97                         \r
98                         int index = 0;\r
99                         BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child("consumers"))\r
100                         {\r
101                                 try\r
102                                 {\r
103                                         const std::string name = xml_consumer.first;\r
104                                         if(name == "ogl")\r
105                                                 channels_.back()->consumer()->add(index++, create_ogl_consumer(xml_consumer.second));                                   \r
106                                         else if(name == "bluefish")                                     \r
107                                                 channels_.back()->consumer()->add(index++, create_bluefish_consumer(xml_consumer.second));                                      \r
108                                         else if(name == "decklink")                                     \r
109                                                 channels_.back()->consumer()->add(index++, create_decklink_consumer(xml_consumer.second));                              \r
110                                         else if(name == "file")                                 \r
111                                                 channels_.back()->consumer()->add(index++, create_ffmpeg_consumer(xml_consumer.second));                                                \r
112                                         else if(name == "audio")\r
113                                                 channels_.back()->consumer()->add(index++, oal_consumer());             \r
114                                         else if(name != "<xmlcomment>")\r
115                                                 CASPAR_LOG(warning) << "Invalid consumer: " << widen(name);     \r
116                                 }\r
117                                 catch(...)\r
118                                 {\r
119                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
120                                 }\r
121                         }                                                       \r
122                 }\r
123         }\r
124                 \r
125         void setup_controllers(const boost::property_tree::ptree& pt)\r
126         {               \r
127                 using boost::property_tree::ptree;\r
128                 BOOST_FOREACH(auto& xml_controller, pt.get_child("configuration.controllers"))\r
129                 {\r
130                         try\r
131                         {\r
132                                 std::string name = xml_controller.first;\r
133                                 std::string protocol = xml_controller.second.get<std::string>("protocol");      \r
134 \r
135                                 if(name == "tcp")\r
136                                 {                                       \r
137                                         unsigned int port = xml_controller.second.get("port", 5250);\r
138                                         auto asyncbootstrapper = make_safe<IO::AsyncEventServer>(create_protocol(protocol), port);\r
139                                         asyncbootstrapper->Start();\r
140                                         async_servers_.push_back(asyncbootstrapper);\r
141                                 }\r
142                                 else\r
143                                         CASPAR_LOG(warning) << "Invalid controller: " << widen(name);   \r
144                         }\r
145                         catch(...)\r
146                         {\r
147                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
148                         }\r
149                 }\r
150         }\r
151 \r
152         safe_ptr<IO::IProtocolStrategy> create_protocol(const std::string& name) const\r
153         {\r
154                 if(name == "AMCP")\r
155                         return make_safe<amcp::AMCPProtocolStrategy>(channels_);\r
156                 else if(name == "CII")\r
157                         return make_safe<cii::CIIProtocolStrategy>(channels_);\r
158                 else if(name == "CLOCK")\r
159                         return make_safe<CLK::CLKProtocolStrategy>(channels_);\r
160                 \r
161                 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info("name") << arg_value_info(name) << msg_info("Invalid protocol"));\r
162         }\r
163 };\r
164 \r
165 server::server() : impl_(new implementation()){}\r
166 \r
167 const std::vector<safe_ptr<channel>> server::get_channels() const\r
168 {\r
169         return impl_->channels_;\r
170 }\r
171 \r
172 }