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