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