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