]> 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) 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 "stdafx.h"\r
22 \r
23 #include "server.h"\r
24 \r
25 #include <accelerator/factory.h>\r
26 #include <accelerator/ogl/factory.h>\r
27 #include <accelerator/cpu/factory.h>\r
28 \r
29 #include <common/env.h>\r
30 #include <common/except.h>\r
31 #include <common/utf.h>\r
32 \r
33 #include <core/video_channel.h>\r
34 #include <core/video_format.h>\r
35 #include <core/producer/stage.h>\r
36 #include <core/consumer/output.h>\r
37 \r
38 #include <modules/bluefish/bluefish.h>\r
39 #include <modules/decklink/decklink.h>\r
40 #include <modules/ffmpeg/ffmpeg.h>\r
41 #include <modules/flash/flash.h>\r
42 #include <modules/oal/oal.h>\r
43 #include <modules/screen/screen.h>\r
44 #include <modules/image/image.h>\r
45 \r
46 #include <modules/oal/consumer/oal_consumer.h>\r
47 #include <modules/bluefish/consumer/bluefish_consumer.h>\r
48 #include <modules/decklink/consumer/decklink_consumer.h>\r
49 #include <modules/screen/consumer/screen_consumer.h>\r
50 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>\r
51 \r
52 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
53 #include <protocol/cii/CIIProtocolStrategy.h>\r
54 #include <protocol/CLK/CLKProtocolStrategy.h>\r
55 #include <protocol/util/AsyncEventServer.h>\r
56 \r
57 #include <boost/algorithm/string.hpp>\r
58 #include <boost/lexical_cast.hpp>\r
59 #include <boost/foreach.hpp>\r
60 #include <boost/property_tree/ptree.hpp>\r
61 #include <boost/property_tree/xml_parser.hpp>\r
62 \r
63 namespace caspar {\r
64 \r
65 using namespace core;\r
66 using namespace protocol;\r
67 \r
68 struct server::impl : boost::noncopyable\r
69 {\r
70         spl::shared_ptr<monitor::subject>                                       event_subject_;\r
71         std::unique_ptr<accelerator::factory>                           accel_factory_;\r
72         std::vector<spl::shared_ptr<IO::AsyncEventServer>>      async_servers_; \r
73         std::vector<spl::shared_ptr<video_channel>>                     channels_;\r
74 \r
75         impl()          \r
76         {       \r
77                 auto accel_str = env::properties().get(L"configuration.accelerator", L"auto");\r
78 \r
79                 if(accel_str == L"cpu")\r
80                         accel_factory_.reset(new accelerator::cpu::factory());\r
81                 else\r
82                 {\r
83                         try\r
84                         {\r
85                                 accel_factory_.reset(new accelerator::ogl::factory());\r
86                         }\r
87                         catch(...)\r
88                         {\r
89                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
90                                 accel_factory_.reset(new accelerator::cpu::factory());\r
91                                 CASPAR_LOG(warning) << L"Using fallback CPU mixer.";\r
92                         }\r
93                 }\r
94 \r
95                 ffmpeg::init();\r
96                 CASPAR_LOG(info) << L"Initialized ffmpeg module.";\r
97                                                           \r
98                 bluefish::init();         \r
99                 CASPAR_LOG(info) << L"Initialized bluefish module.";\r
100                                                           \r
101                 decklink::init();         \r
102                 CASPAR_LOG(info) << L"Initialized decklink module.";\r
103                                                                                                                   \r
104                 oal::init();              \r
105                 CASPAR_LOG(info) << L"Initialized oal module.";\r
106                                                           \r
107                 screen::init();           \r
108                 CASPAR_LOG(info) << L"Initialized ogl module.";\r
109 \r
110                 image::init();            \r
111                 CASPAR_LOG(info) << L"Initialized image module.";\r
112 \r
113                 flash::init();            \r
114                 CASPAR_LOG(info) << L"Initialized flash module.";\r
115 \r
116                 setup_channels(env::properties());\r
117                 CASPAR_LOG(info) << L"Initialized channels.";\r
118 \r
119                 setup_controllers(env::properties());\r
120                 CASPAR_LOG(info) << L"Initialized controllers.";\r
121         }\r
122 \r
123         ~impl()\r
124         {               \r
125                 image::uninit();\r
126                 ffmpeg::uninit();\r
127 \r
128                 async_servers_.clear();\r
129                 channels_.clear();\r
130         }\r
131                                 \r
132         void setup_channels(const boost::property_tree::wptree& pt)\r
133         {   \r
134                 using boost::property_tree::wptree;\r
135                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))\r
136                 {               \r
137                         auto format_desc = video_format_desc(xml_channel.second.get(L"video-mode", L"PAL"));            \r
138                         if(format_desc.format == video_format::invalid)\r
139                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));\r
140                         \r
141                         auto channel = spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), format_desc, accel_factory_->create_image_mixer());\r
142                         \r
143                         BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))\r
144                         {\r
145                                 try\r
146                                 {\r
147                                         auto name = xml_consumer.first;\r
148                                         if(name == L"screen")\r
149                                                 channel->output().add(caspar::screen::create_consumer(xml_consumer.second));                                    \r
150                                         else if(name == L"bluefish")                                    \r
151                                                 channel->output().add(bluefish::create_consumer(xml_consumer.second));                                  \r
152                                         else if(name == L"decklink")                                    \r
153                                                 channel->output().add(decklink::create_consumer(xml_consumer.second));                          \r
154                                         else if(name == L"file")                                        \r
155                                                 channel->output().add(ffmpeg::create_consumer(xml_consumer.second));                                            \r
156                                         else if(name == L"system-audio")\r
157                                                 channel->output().add(oal::create_consumer());          \r
158                                         else if(name != L"<xmlcomment>")\r
159                                                 CASPAR_LOG(warning) << "Invalid consumer: " << name;    \r
160                                 }\r
161                                 catch(...)\r
162                                 {\r
163                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
164                                 }\r
165                         }               \r
166 \r
167                     channel->subscribe(monitor::observable::observer_ptr(event_subject_));\r
168                         channels_.push_back(channel);\r
169                 }\r
170 \r
171                 // Dummy diagnostics channel\r
172                 if(env::properties().get(L"configuration.channel-grid", false))\r
173                         channels_.push_back(spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), core::video_format_desc(core::video_format::x576p2500), accel_factory_->create_image_mixer()));\r
174         }\r
175                 \r
176         void setup_controllers(const boost::property_tree::wptree& pt)\r
177         {               \r
178                 using boost::property_tree::wptree;\r
179                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))\r
180                 {\r
181                         try\r
182                         {\r
183                                 auto name = xml_controller.first;\r
184                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   \r
185 \r
186                                 if(name == L"tcp")\r
187                                 {                                       \r
188                                         unsigned int port = xml_controller.second.get(L"port", 5250);\r
189                                         auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(create_protocol(protocol), port);\r
190                                         asyncbootstrapper->Start();\r
191                                         async_servers_.push_back(asyncbootstrapper);\r
192                                 }\r
193                                 else\r
194                                         CASPAR_LOG(warning) << "Invalid controller: " << name;  \r
195                         }\r
196                         catch(...)\r
197                         {\r
198                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
199                         }\r
200                 }\r
201         }\r
202 \r
203         spl::shared_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const\r
204         {\r
205                 if(boost::iequals(name, L"AMCP"))\r
206                         return spl::make_shared<amcp::AMCPProtocolStrategy>(channels_);\r
207                 else if(boost::iequals(name, L"CII"))\r
208                         return spl::make_shared<cii::CIIProtocolStrategy>(channels_);\r
209                 else if(boost::iequals(name, L"CLOCK"))\r
210                         return spl::make_shared<CLK::CLKProtocolStrategy>(channels_);\r
211                 \r
212                 BOOST_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol"));\r
213         }\r
214 };\r
215 \r
216 server::server() : impl_(new impl()){}\r
217 \r
218 const std::vector<spl::shared_ptr<video_channel>> server::get_channels() const\r
219 {\r
220         return impl_->channels_;\r
221 }\r
222 void server::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_->subscribe(o);}\r
223 void server::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_->unsubscribe(o);}\r
224 \r
225 }