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