]> git.sesse.net Git - casparcg/blob - shell/server.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / shell / server.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21 #include "stdafx.h"
22
23 #include "server.h"
24
25 #include <accelerator/accelerator.h>
26
27 #include <common/env.h>
28 #include <common/except.h>
29 #include <common/utf.h>
30
31 #include <core/video_channel.h>
32 #include <core/video_format.h>
33 #include <core/producer/stage.h>
34 #include <core/consumer/output.h>
35
36 #include <modules/bluefish/bluefish.h>
37 #include <modules/decklink/decklink.h>
38 #include <modules/ffmpeg/ffmpeg.h>
39 #include <modules/flash/flash.h>
40 #include <modules/oal/oal.h>
41 #include <modules/screen/screen.h>
42 #include <modules/image/image.h>
43
44 #include <modules/oal/consumer/oal_consumer.h>
45 #include <modules/bluefish/consumer/bluefish_consumer.h>
46 #include <modules/decklink/consumer/decklink_consumer.h>
47 #include <modules/screen/consumer/screen_consumer.h>
48 #include <modules/ffmpeg/consumer/ffmpeg_consumer.h>
49
50 #include <protocol/amcp/AMCPProtocolStrategy.h>
51 #include <protocol/cii/CIIProtocolStrategy.h>
52 #include <protocol/CLK/CLKProtocolStrategy.h>
53 #include <protocol/util/AsyncEventServer.h>
54
55 #include <boost/algorithm/string.hpp>
56 #include <boost/lexical_cast.hpp>
57 #include <boost/foreach.hpp>
58 #include <boost/property_tree/ptree.hpp>
59 #include <boost/property_tree/xml_parser.hpp>
60
61 namespace caspar {
62
63 using namespace core;
64 using namespace protocol;
65
66 struct server::impl : boost::noncopyable
67 {
68         monitor::basic_subject                                                          event_subject_;
69         accelerator::accelerator                                                        accelerator_;
70         std::vector<spl::shared_ptr<IO::AsyncEventServer>>      async_servers_; 
71         std::vector<spl::shared_ptr<video_channel>>                     channels_;
72
73         impl()          
74                 : accelerator_(env::properties().get(L"configuration.accelerator", L"auto"))
75         {       
76
77                 ffmpeg::init();
78                 CASPAR_LOG(info) << L"Initialized ffmpeg module.";
79                                                           
80                 bluefish::init();         
81                 CASPAR_LOG(info) << L"Initialized bluefish module.";
82                                                           
83                 decklink::init();         
84                 CASPAR_LOG(info) << L"Initialized decklink module.";
85                                                                                                                   
86                 oal::init();              
87                 CASPAR_LOG(info) << L"Initialized oal module.";
88                                                           
89                 screen::init();           
90                 CASPAR_LOG(info) << L"Initialized ogl module.";
91
92                 image::init();            
93                 CASPAR_LOG(info) << L"Initialized image module.";
94
95                 flash::init();            
96                 CASPAR_LOG(info) << L"Initialized flash module.";
97
98                 setup_channels(env::properties());
99                 CASPAR_LOG(info) << L"Initialized channels.";
100
101                 setup_controllers(env::properties());
102                 CASPAR_LOG(info) << L"Initialized controllers.";
103         }
104
105         ~impl()
106         {               
107                 async_servers_.clear();
108                 channels_.clear();
109
110                 Sleep(500); // HACK: Wait for asynchronous destruction of producers and consumers.
111
112                 image::uninit();
113                 ffmpeg::uninit();
114         }
115                                 
116         void setup_channels(const boost::property_tree::wptree& pt)
117         {   
118                 using boost::property_tree::wptree;
119                 BOOST_FOREACH(auto& xml_channel, pt.get_child(L"configuration.channels"))
120                 {               
121                         auto format_desc = video_format_desc(xml_channel.second.get(L"video-mode", L"PAL"));            
122                         if(format_desc.format == video_format::invalid)
123                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Invalid video-mode."));
124                         
125                         auto channel = spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), format_desc, accelerator_.create_image_mixer());
126                         
127                         BOOST_FOREACH(auto& xml_consumer, xml_channel.second.get_child(L"consumers"))
128                         {
129                                 try
130                                 {
131                                         auto name = xml_consumer.first;
132                                         if(name == L"screen")
133                                                 channel->output().add(caspar::screen::create_consumer(xml_consumer.second));                                    
134                                         else if(name == L"bluefish")                                    
135                                                 channel->output().add(bluefish::create_consumer(xml_consumer.second));                                  
136                                         else if(name == L"decklink")                                    
137                                                 channel->output().add(decklink::create_consumer(xml_consumer.second));                          
138                                         else if(name == L"file")                                        
139                                                 channel->output().add(ffmpeg::create_consumer(xml_consumer.second));                                            
140                                         else if(name == L"system-audio")
141                                                 channel->output().add(oal::create_consumer());          
142                                         else if(name != L"<xmlcomment>")
143                                                 CASPAR_LOG(warning) << "Invalid consumer: " << name;    
144                                 }
145                                 catch(...)
146                                 {
147                                         CASPAR_LOG_CURRENT_EXCEPTION();
148                                 }
149                         }               
150
151                     channel->subscribe(monitor::observable::observer_ptr(event_subject_));
152                         channels_.push_back(channel);
153                 }
154
155                 // Dummy diagnostics channel
156                 if(env::properties().get(L"configuration.channel-grid", false))
157                         channels_.push_back(spl::make_shared<video_channel>(static_cast<int>(channels_.size()+1), core::video_format_desc(core::video_format::x576p2500), accelerator_.create_image_mixer()));
158         }
159                 
160         void setup_controllers(const boost::property_tree::wptree& pt)
161         {               
162                 using boost::property_tree::wptree;
163                 BOOST_FOREACH(auto& xml_controller, pt.get_child(L"configuration.controllers"))
164                 {
165                         try
166                         {
167                                 auto name = xml_controller.first;
168                                 auto protocol = xml_controller.second.get<std::wstring>(L"protocol");   
169
170                                 if(name == L"tcp")
171                                 {                                       
172                                         unsigned int port = xml_controller.second.get(L"port", 5250);
173                                         auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(create_protocol(protocol), port);
174                                         async_servers_.push_back(asyncbootstrapper);
175                                 }
176                                 else
177                                         CASPAR_LOG(warning) << "Invalid controller: " << name;  
178                         }
179                         catch(...)
180                         {
181                                 CASPAR_LOG_CURRENT_EXCEPTION();
182                         }
183                 }
184         }
185
186         spl::shared_ptr<IO::IProtocolStrategy> create_protocol(const std::wstring& name) const
187         {
188                 if(boost::iequals(name, L"AMCP"))
189                         return spl::make_shared<amcp::AMCPProtocolStrategy>(channels_);
190                 else if(boost::iequals(name, L"CII"))
191                         return spl::make_shared<cii::CIIProtocolStrategy>(channels_);
192                 else if(boost::iequals(name, L"CLOCK"))
193                         return spl::make_shared<CLK::CLKProtocolStrategy>(channels_);
194                 
195                 CASPAR_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol"));
196         }
197 };
198
199 server::server() : impl_(new impl()){}
200
201 const std::vector<spl::shared_ptr<video_channel>> server::channels() const
202 {
203         return impl_->channels_;
204 }
205 void server::subscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.subscribe(o);}
206 void server::unsubscribe(const monitor::observable::observer_ptr& o){impl_->event_subject_.unsubscribe(o);}
207
208 }