]> git.sesse.net Git - casparcg/blob - shell/main.cpp
[ffmpeg] Fixed problem where pan audio filter was not correctly parsed in Linux becau...
[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 #include <boost/algorithm/string/split.hpp>
65 #include <boost/algorithm/string/classification.hpp>
66
67 #include <tbb/atomic.h>
68
69 #include <future>
70 #include <set>
71
72 #include <csignal>
73
74 using namespace caspar;
75         
76 void setup_global_locale()
77 {
78         boost::locale::generator gen;
79         gen.categories(boost::locale::codepage_facet);
80
81         std::locale::global(gen(""));
82
83         // sscanf is used in for example FFmpeg where we want decimals to be parsed as .
84         std::setlocale(LC_ALL, "C");
85 }
86
87 void print_info()
88 {
89         CASPAR_LOG(info) << L"############################################################################";
90         CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";
91         CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";
92         CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";
93         CASPAR_LOG(info) << L"http://www.casparcg.com/";
94         CASPAR_LOG(info) << L"############################################################################";
95         CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();
96         CASPAR_LOG(info) << L"on " << os_description();
97         CASPAR_LOG(info) << cpu_info();
98         CASPAR_LOG(info) << system_product_name();
99 }
100
101
102 void print_system_info(const spl::shared_ptr<core::system_info_provider_repository>& repo)
103 {
104         boost::property_tree::wptree info;
105         repo->fill_information(info);
106
107         for (auto& elem : info.get_child(L"system"))
108                 log::print_child(boost::log::trivial::info, L"", elem.first, elem.second);
109 }
110
111 void do_run(
112                 std::weak_ptr<caspar::IO::protocol_strategy<wchar_t>> amcp,
113                 std::promise<bool>& shutdown_server_now,
114                 tbb::atomic<bool>& should_wait_for_keypress)
115 {
116         std::wstring wcmd;
117         while(true)
118         {
119                 std::getline(std::wcin, wcmd); // TODO: It's blocking...
120                                 
121                 //boost::to_upper(wcmd);
122
123                 if(boost::iequals(wcmd, L"EXIT") || boost::iequals(wcmd, L"Q") || boost::iequals(wcmd, L"QUIT") || boost::iequals(wcmd, L"BYE"))
124                 {
125                         CASPAR_LOG(info) << L"Received message from Console: " << wcmd << L"\\r\\n";
126                         should_wait_for_keypress = true;
127                         shutdown_server_now.set_value(false);   //false to not restart
128                         break;
129                 }
130
131                 try
132                 {
133                         // This is just dummy code for testing.
134                         if(wcmd.substr(0, 1) == L"1")
135                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP \r\nPLAY 1-1";
136                         else if(wcmd.substr(0, 1) == L"2")
137                                 wcmd = L"MIXER 1-0 VIDEO IS_KEY 1";
138                         else if(wcmd.substr(0, 1) == L"3")
139                                 wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";
140                         else if(wcmd.substr(0, 1) == L"4")
141                                 wcmd = L"PLAY 1-1 DV FILTER yadif=1:-1 LOOP";
142                         else if(wcmd.substr(0, 1) == L"5")
143                         {
144                                 auto file = wcmd.substr(2, wcmd.length()-1);
145                                 wcmd = L"PLAY 1-1 " + file + L" LOOP\r\n" 
146                                                 L"PLAY 1-2 " + file + L" LOOP\r\n" 
147                                                 L"PLAY 1-3 " + file + L" LOOP\r\n"
148                                                 L"PLAY 2-1 " + file + L" LOOP\r\n" 
149                                                 L"PLAY 2-2 " + file + L" LOOP\r\n" 
150                                                 L"PLAY 2-3 " + file + L" LOOP\r\n";
151                         }
152                         else if(wcmd.substr(0, 1) == L"7")
153                         {
154                                 wcmd = L"";
155                                 wcmd += L"CLEAR 1\r\n";
156                                 wcmd += L"MIXER 1 CLEAR\r\n";
157                                 wcmd += L"PLAY 1-0 GREEN\r\n";
158                                 wcmd += L"PLAY 1-1 BLUE\r\n";
159                                 wcmd += L"CG 1-2 ADD 1 ECS_TEST 1\r\n";
160                                 wcmd += L"MIXER 1-2 FILL 0 -1 1 2\r\n";
161                         }
162                         else if(wcmd.substr(0, 1) == L"8")
163                         {
164                                 wcmd = L"";
165                                 wcmd += L"MIXER 1-1 FILL 0.0 0.5 1.0 1.0 500 linear DEFER\r\n";
166                                 wcmd += L"MIXER 1-2 FILL 0.0 0.0 1.0 1.0 500 linear DEFER\r\n";
167                                 wcmd += L"MIXER 1 COMMIT\r\n";
168                         }
169                         else if(wcmd.substr(0, 1) == L"X")
170                         {
171                                 int num = 0;
172                                 std::wstring file;
173                                 try
174                                 {
175                                         num = boost::lexical_cast<int>(wcmd.substr(1, 2));
176                                         file = wcmd.substr(4, wcmd.length()-1);
177                                 }
178                                 catch(...)
179                                 {
180                                         num = boost::lexical_cast<int>(wcmd.substr(1, 1));
181                                         file = wcmd.substr(3, wcmd.length()-1);
182                                 }
183
184                                 int n = 0;
185                                 int num2 = num;
186                                 while(num2 > 0)
187                                 {
188                                         num2 >>= 1;
189                                         n++;
190                                 }
191
192                                 wcmd = L"MIXER 1 GRID " + boost::lexical_cast<std::wstring>(n);
193
194                                 for(int i = 1; i <= num; ++i)
195                                         wcmd += L"\r\nPLAY 1-" + boost::lexical_cast<std::wstring>(i) + L" " + file + L" LOOP";// + L" SLIDE 100 LOOP";
196                         }
197                 }
198                 catch (...)
199                 {
200                         CASPAR_LOG_CURRENT_EXCEPTION();
201                         continue;
202                 }
203
204                 wcmd += L"\r\n";
205                 auto strong = amcp.lock();
206                 if (strong)
207                         strong->parse(wcmd);
208                 else
209                         break;
210         }
211 };
212
213 bool run(const std::wstring& config_file_name, tbb::atomic<bool>& should_wait_for_keypress)
214 {
215         std::promise<bool> shutdown_server_now;
216         std::future<bool> shutdown_server = shutdown_server_now.get_future();
217
218         print_info();
219
220         // Create server object which initializes channels, protocols and controllers.
221         std::unique_ptr<server> caspar_server(new server(shutdown_server_now));
222
223         // For example CEF resets the global locale, so this is to reset it back to "our" preference.
224         setup_global_locale();
225
226         // Print environment information.
227         print_system_info(caspar_server->get_system_info_provider_repo());
228
229         std::wstringstream str;
230         boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
231         boost::property_tree::write_xml(str, env::properties(), w);
232         CASPAR_LOG(info) << config_file_name << L":\n-----------------------------------------\n" << str.str() << L"-----------------------------------------";
233         
234         {
235                 CASPAR_SCOPED_CONTEXT_MSG(config_file_name + L": ")
236                 caspar_server->start();
237         }
238
239         // Create a dummy client which prints amcp responses to console.
240         auto console_client = spl::make_shared<IO::ConsoleClientInfo>();
241
242         // Create a amcp parser for console commands.
243         std::shared_ptr<IO::protocol_strategy<wchar_t>> amcp = spl::make_shared<caspar::IO::delimiter_based_chunking_strategy_factory<wchar_t>>(
244                         L"\r\n",
245                         spl::make_shared<caspar::IO::legacy_strategy_adapter_factory>(
246                                         spl::make_shared<protocol::amcp::AMCPProtocolStrategy>(
247                                                         L"Console",
248                                                         caspar_server->get_amcp_command_repository())))->create(console_client);
249         std::weak_ptr<IO::protocol_strategy<wchar_t>> weak_amcp = amcp;
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, weak_amcp, std::ref(shutdown_server_now), std::ref(should_wait_for_keypress)));    //compiler didn't like lambda here...
254         stdin_thread.detach();
255         bool should_restart = shutdown_server.get();
256         amcp.reset();
257
258         while (weak_amcp.lock());
259
260         return should_restart;
261 }
262
263 void on_abort(int)
264 {
265         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("abort called"));
266 }
267
268 int main(int argc, char** argv)
269 {
270         if (intercept_command_line_args(argc, argv))
271                 return 0;
272
273         int return_code = 0;
274         setup_prerequisites();
275         //std::signal(SIGABRT, on_abort);
276
277         setup_global_locale();
278
279         std::wcout << L"Type \"q\" to close application." << std::endl;
280         
281         // Set debug mode.
282         auto debugging_environment = setup_debugging_environment();
283
284         // Increase process priotity.
285         increase_process_priority();
286
287         // Install general protection fault handler.
288         ensure_gpf_handler_installed_for_thread("main thread");
289
290         // Install GPF handler into all tbb threads.
291         struct tbb_thread_installer : public tbb::task_scheduler_observer
292         {
293                 tbb_thread_installer(){observe(true);}
294                 void on_scheduler_entry(bool is_worker) override
295                 {
296                         ensure_gpf_handler_installed_for_thread("tbb-worker-thread");
297                 }
298         } tbb_thread_installer;
299
300         tbb::task_scheduler_init init;
301         std::wstring config_file_name(L"casparcg.config");
302         
303         try 
304         {
305                 // Configure environment properties from configuration.
306                 if (argc >= 2)
307                         config_file_name = caspar::u16(argv[1]);
308
309                 env::configure(config_file_name);
310
311                 log::set_log_level(env::properties().get(L"configuration.log-level", L"info"));
312                 auto log_categories_str = env::properties().get(L"configuration.log-categories", L"communication");
313                 std::set<std::wstring> log_categories;
314                 boost::split(log_categories, log_categories_str, boost::is_any_of(L", "));
315                 for (auto& log_category : { L"calltrace", L"communication" })
316                         log::set_log_category(log_category, log_categories.find(log_category) != log_categories.end());
317
318                 if (env::properties().get(L"configuration.debugging.remote", false))
319                         wait_for_remote_debugging();
320
321                 // Start logging to file.
322                 log::add_file_sink(env::log_folder() + L"caspar",               caspar::log::category != caspar::log::log_category::calltrace);
323                 log::add_file_sink(env::log_folder() + L"calltrace",    caspar::log::category == caspar::log::log_category::calltrace);
324                 std::wcout << L"Logging [info] or higher severity to " << env::log_folder() << std::endl << std::endl;
325                 
326                 // Setup console window.
327                 setup_console_window();
328
329                 tbb::atomic<bool> should_wait_for_keypress;
330                 should_wait_for_keypress = false;
331                 auto should_restart = run(config_file_name, should_wait_for_keypress);
332                 return_code = should_restart ? 5 : 0;
333
334                 for (auto& thread : get_thread_infos())
335                 {
336                         if (thread->name != "main thread" && thread->name != "tbb-worker-thread")
337                                 CASPAR_LOG(warning) << L"Thread left running: " << thread->name << L" (" << thread->native_id << L")";
338                 }
339                 
340                 CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";
341
342                 if (should_wait_for_keypress)
343                         wait_for_keypress();
344         }
345         catch(const boost::property_tree::file_parser_error& e)
346         {
347                 CASPAR_LOG_CURRENT_EXCEPTION();
348                 CASPAR_LOG(fatal) << "At " << u8(config_file_name) << ":" << e.line() << ": " << e.message() << ". Please check the configuration file (" << u8(config_file_name) << ") for errors.";
349                 wait_for_keypress();
350         }
351         catch (const user_error& e)
352         {
353                 CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(debug);
354                 CASPAR_LOG(fatal) << get_message_and_context(e) << " Please check the configuration file (" << u8(config_file_name) << ") for errors. Turn on log level debug for stacktrace.";
355                 wait_for_keypress();
356         }
357         catch(...)
358         {
359                 CASPAR_LOG_CURRENT_EXCEPTION();
360                 CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";
361                 boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
362                 std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";
363                 boost::this_thread::sleep_for(boost::chrono::milliseconds(4000));
364         }
365
366         return return_code;
367 }