]> git.sesse.net Git - casparcg/blob - shell/main.cpp
* Working server startup in Linux
[casparcg] / shell / main.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
22 // tbbmalloc_proxy: 
23 // Replace the standard memory allocation routines in Microsoft* C/C++ RTL 
24 // (malloc/free, global new/delete, etc.) with the TBB memory allocator. 
25
26 #include "stdafx.h"
27
28 #include <tbb/task_scheduler_init.h>
29 #include <tbb/task_scheduler_observer.h>
30
31 #if defined _DEBUG && defined _MSC_VER
32         #define _CRTDBG_MAP_ALLOC
33         #include <stdlib.h>
34         #include <crtdbg.h>
35 #else
36         #include <tbb/tbbmalloc_proxy.h>
37 #endif
38
39 #include "server.h"
40 #include "platform_specific.h"
41
42 #include <protocol/util/strategy_adapters.h>
43 #include <protocol/amcp/AMCPProtocolStrategy.h>
44
45 #include <common/env.h>
46 #include <common/except.h>
47 #include <common/log.h>
48 #include <common/gl/gl_check.h>
49 #include <common/os/system_info.h>
50 #include <common/os/general_protection_fault.h>
51
52 #include <core/system_info_provider.h>
53
54 #include <boost/property_tree/detail/file_parser_error.hpp>
55 #include <boost/property_tree/xml_parser.hpp>
56 #include <boost/locale.hpp>
57 #include <boost/lexical_cast.hpp>
58 #include <boost/algorithm/string/predicate.hpp>
59 #include <boost/thread.hpp>
60 #include <boost/thread/future.hpp>
61 #include <boost/algorithm/string/case_conv.hpp>
62
63 #include <future>
64
65 #include <csignal>
66
67 using namespace caspar;
68         
69 void setup_global_locale()
70 {
71         boost::locale::generator gen;
72         gen.categories(boost::locale::codepage_facet);
73
74         std::locale::global(gen(""));
75 }
76
77 void print_info()
78 {
79         CASPAR_LOG(info) << L"############################################################################";
80         CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";
81         CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";
82         CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";
83         CASPAR_LOG(info) << L"http://www.casparcg.com/";
84         CASPAR_LOG(info) << L"############################################################################";
85         CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();
86         CASPAR_LOG(info) << L"on " << os_description();
87         CASPAR_LOG(info) << cpu_info();
88         CASPAR_LOG(info) << system_product_name();
89 }
90
91 void print_child(const std::wstring& indent, const std::wstring& elem, const boost::property_tree::wptree& tree)
92 {
93         auto data = tree.data();
94
95         if (data.empty())
96                 CASPAR_LOG(info) << indent << elem;
97         else
98                 CASPAR_LOG(info) << indent << elem << L" " << tree.data();
99
100         for (auto& child : tree)
101                 print_child(indent + L"    ", child.first, child.second);
102 }
103
104 void print_system_info(const spl::shared_ptr<core::system_info_provider_repository>& repo)
105 {
106         boost::property_tree::wptree info;
107         repo->fill_information(info);
108
109         for (auto& elem : info.get_child(L"system"))
110                 print_child(L"", elem.first, elem.second);
111 }
112
113 void do_run(server& caspar_server, std::promise<bool>& shutdown_server_now)
114 {
115         // Create a dummy client which prints amcp responses to console.
116         auto console_client = spl::make_shared<IO::ConsoleClientInfo>();
117         
118         // Create a amcp parser for console commands.
119         auto amcp = spl::make_shared<caspar::IO::delimiter_based_chunking_strategy_factory<wchar_t>>(
120                         L"\r\n",
121                         spl::make_shared<caspar::IO::legacy_strategy_adapter_factory>(
122                                         spl::make_shared<protocol::amcp::AMCPProtocolStrategy>(
123                                                         caspar_server.channels(),
124                                                         caspar_server.get_thumbnail_generator(),
125                                                         caspar_server.get_media_info_repo(),
126                                                         caspar_server.get_system_info_provider_repo(),
127                                                         caspar_server.get_cg_registry(),
128                                                         shutdown_server_now)))->create(console_client);
129
130         std::wstring wcmd;
131         while(true)
132         {
133                 std::getline(std::wcin, wcmd); // TODO: It's blocking...
134                                 
135                 //boost::to_upper(wcmd);
136
137                 if(boost::iequals(wcmd, L"EXIT") || boost::iequals(wcmd, L"Q") || boost::iequals(wcmd, L"QUIT") || boost::iequals(wcmd, L"BYE"))
138                 {
139                         shutdown_server_now.set_value(true);    //true to wait for keypress
140                         break;
141                 }
142
143                 try
144                 {
145                         // This is just dummy code for testing.
146                         if(wcmd.substr(0, 1) == L"1")
147                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP \r\nPLAY 1-1";
148                         else if(wcmd.substr(0, 1) == L"2")
149                                 wcmd = L"MIXER 1-0 VIDEO IS_KEY 1";
150                         else if(wcmd.substr(0, 1) == L"3")
151                                 wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";
152                         else if(wcmd.substr(0, 1) == L"4")
153                                 wcmd = L"PLAY 1-1 DV FILTER yadif=1:-1 LOOP";
154                         else if(wcmd.substr(0, 1) == L"5")
155                         {
156                                 auto file = wcmd.substr(2, wcmd.length()-1);
157                                 wcmd = L"PLAY 1-1 " + file + L" LOOP\r\n" 
158                                                 L"PLAY 1-2 " + file + L" LOOP\r\n" 
159                                                 L"PLAY 1-3 " + file + L" LOOP\r\n"
160                                                 L"PLAY 2-1 " + file + L" LOOP\r\n" 
161                                                 L"PLAY 2-2 " + file + L" LOOP\r\n" 
162                                                 L"PLAY 2-3 " + file + L" LOOP\r\n";
163                         }
164                         else if(wcmd.substr(0, 1) == L"7")
165                         {
166                                 wcmd = L"";
167                                 wcmd += L"CLEAR 1\r\n";
168                                 wcmd += L"MIXER 1 CLEAR\r\n";
169                                 wcmd += L"PLAY 1-0 GREEN\r\n";
170                                 wcmd += L"PLAY 1-1 BLUE\r\n";
171                                 wcmd += L"CG 1-2 ADD 1 ECS_TEST 1\r\n";
172                                 wcmd += L"MIXER 1-2 FILL 0 -1 1 2\r\n";
173                         }
174                         else if(wcmd.substr(0, 1) == L"8")
175                         {
176                                 wcmd = L"";
177                                 wcmd += L"MIXER 1-1 FILL 0.0 0.5 1.0 1.0 500 linear DEFER\r\n";
178                                 wcmd += L"MIXER 1-2 FILL 0.0 0.0 1.0 1.0 500 linear DEFER\r\n";
179                                 wcmd += L"MIXER 1 COMMIT\r\n";
180                         }
181                         else if(wcmd.substr(0, 1) == L"X")
182                         {
183                                 int num = 0;
184                                 std::wstring file;
185                                 try
186                                 {
187                                         num = boost::lexical_cast<int>(wcmd.substr(1, 2));
188                                         file = wcmd.substr(4, wcmd.length()-1);
189                                 }
190                                 catch(...)
191                                 {
192                                         num = boost::lexical_cast<int>(wcmd.substr(1, 1));
193                                         file = wcmd.substr(3, wcmd.length()-1);
194                                 }
195
196                                 int n = 0;
197                                 int num2 = num;
198                                 while(num2 > 0)
199                                 {
200                                         num2 >>= 1;
201                                         n++;
202                                 }
203
204                                 wcmd = L"MIXER 1 GRID " + boost::lexical_cast<std::wstring>(n);
205
206                                 for(int i = 1; i <= num; ++i)
207                                         wcmd += L"\r\nPLAY 1-" + boost::lexical_cast<std::wstring>(i) + L" " + file + L" LOOP";// + L" SLIDE 100 LOOP";
208                         }
209                 }
210                 catch (...)
211                 {
212                         CASPAR_LOG_CURRENT_EXCEPTION();
213                         continue;
214                 }
215
216                 wcmd += L"\r\n";
217                 amcp->parse(wcmd);
218         }
219 };
220
221 bool run()
222 {
223         std::promise<bool> shutdown_server_now;
224         std::future<bool> shutdown_server = shutdown_server_now.get_future();
225
226         print_info();
227
228         // Create server object which initializes channels, protocols and controllers.
229         server caspar_server(shutdown_server_now);
230         
231         // Print environment information.
232         print_system_info(caspar_server.get_system_info_provider_repo());
233
234         std::wstringstream str;
235         boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
236         boost::property_tree::write_xml(str, env::properties(), w);
237         CASPAR_LOG(info) << L"casparcg.config:\n-----------------------------------------\n" << str.str().c_str() << L"-----------------------------------------";
238         
239         caspar_server.start();
240
241         //auto console_obs = reactive::make_observer([](const monitor::event& e)
242         //{
243         //      std::stringstream str;
244         //      str << e;
245         //      CASPAR_LOG(trace) << str.str().c_str();
246         //});
247
248         //caspar_server.subscribe(console_obs);
249
250
251         // Use separate thread for the blocking console input, will be terminated 
252         // anyway when the main thread terminates.
253         boost::thread stdin_thread(std::bind(do_run, std::ref(caspar_server), std::ref(shutdown_server_now)));  //compiler didn't like lambda here...
254         stdin_thread.detach();
255         return shutdown_server.get();
256 }
257
258 void on_abort(int)
259 {
260         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("abort called"));
261 }
262
263 int main(int argc, wchar_t* argv[])
264 {       
265         int return_code = 0;
266         setup_prerequisites();
267         std::signal(SIGABRT, on_abort);
268
269         setup_global_locale();
270
271         std::wcout << L"Type \"q\" to close application." << std::endl;
272         
273         // Set debug mode.
274         auto debugging_environment = setup_debugging_environment();
275
276         // Increase process priotity.
277         increase_process_priority();
278
279         // Install general protection fault handler.
280         ensure_gpf_handler_installed_for_thread("main thread");
281
282         // Install GPF handler into all tbb threads.
283         struct tbb_thread_installer : public tbb::task_scheduler_observer
284         {
285                 tbb_thread_installer(){observe(true);}
286                 void on_scheduler_entry(bool is_worker)
287                 {
288                         ensure_gpf_handler_installed_for_thread("tbb-worker-thread");
289                 }
290         } tbb_thread_installer;
291
292         tbb::task_scheduler_init init;
293         
294         try 
295         {
296                 // Configure environment properties from configuration.
297                 env::configure(L"casparcg.config");
298
299                 log::set_log_level(env::properties().get(L"configuration.log-level", L"debug"));
300
301                 if (env::properties().get(L"configuration.debugging.remote", false))
302                         wait_for_remote_debugging();
303
304                 // Start logging to file.
305                 log::add_file_sink(env::log_folder());                  
306                 std::wcout << L"Logging [info] or higher severity to " << env::log_folder() << std::endl << std::endl;
307                 
308                 // Setup console window.
309                 setup_console_window();
310
311                 return_code = run() ? 5 : 0;
312                 
313                 boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
314                 CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";
315         }
316         catch(boost::property_tree::file_parser_error&)
317         {
318                 CASPAR_LOG_CURRENT_EXCEPTION();
319                 CASPAR_LOG(fatal) << L"Unhandled configuration error in main thread. Please check the configuration file (casparcg.config) for errors.";
320                 wait_for_keypress();
321         }
322         catch(...)
323         {
324                 CASPAR_LOG_CURRENT_EXCEPTION();
325                 CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";
326                 boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
327                 std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";
328                 boost::this_thread::sleep_for(boost::chrono::milliseconds(4000));
329         }
330         
331         return return_code;
332 }