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