2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
23 // Replace the standard memory allocation routines in Microsoft* C/C++ RTL
24 // (malloc/free, global new/delete, etc.) with the TBB memory allocator.
28 #include <tbb/task_scheduler_init.h>
29 #include <tbb/task_scheduler_observer.h>
31 #if defined _DEBUG && defined _MSC_VER
32 #define _CRTDBG_MAP_ALLOC
36 // Reenable when tbb gets official support for vc14
37 //#include <tbb/tbbmalloc_proxy.h>
41 #include "platform_specific.h"
42 #include "included_modules.h"
44 #include <protocol/util/strategy_adapters.h>
45 #include <protocol/amcp/AMCPProtocolStrategy.h>
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>
54 #include <core/system_info_provider.h>
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>
67 #include <tbb/atomic.h>
75 using namespace caspar;
77 void setup_global_locale()
79 boost::locale::generator gen;
80 gen.categories(boost::locale::codepage_facet);
82 std::locale::global(gen(""));
84 // sscanf is used in for example FFmpeg where we want decimals to be parsed as .
85 std::setlocale(LC_ALL, "C");
90 CASPAR_LOG(info) << L"############################################################################";
91 CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";
92 CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";
93 CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";
94 CASPAR_LOG(info) << L"http://www.casparcg.com/";
95 CASPAR_LOG(info) << L"############################################################################";
96 CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();
97 CASPAR_LOG(info) << L"on " << os_description();
98 CASPAR_LOG(info) << cpu_info();
99 CASPAR_LOG(info) << system_product_name();
103 void print_system_info(const spl::shared_ptr<core::system_info_provider_repository>& repo)
105 boost::property_tree::wptree info;
106 repo->fill_information(info);
108 for (auto& elem : info.get_child(L"system"))
109 log::print_child(boost::log::trivial::info, L"", elem.first, elem.second);
113 std::weak_ptr<caspar::IO::protocol_strategy<wchar_t>> amcp,
114 std::promise<bool>& shutdown_server_now,
115 tbb::atomic<bool>& should_wait_for_keypress)
117 ensure_gpf_handler_installed_for_thread("Console thread");
121 std::getline(std::wcin, wcmd); // TODO: It's blocking...
123 //boost::to_upper(wcmd);
125 if(boost::iequals(wcmd, L"EXIT") || boost::iequals(wcmd, L"Q") || boost::iequals(wcmd, L"QUIT") || boost::iequals(wcmd, L"BYE"))
127 CASPAR_LOG(info) << L"Received message from Console: " << wcmd << L"\\r\\n";
128 should_wait_for_keypress = true;
129 shutdown_server_now.set_value(false); //false to not restart
135 // This is just dummy code for testing.
136 if(wcmd.substr(0, 1) == L"1")
137 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP \r\nPLAY 1-1";
138 else if(wcmd.substr(0, 1) == L"2")
139 wcmd = L"MIXER 1-0 VIDEO IS_KEY 1";
140 else if(wcmd.substr(0, 1) == L"3")
141 wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";
142 else if(wcmd.substr(0, 1) == L"4")
143 wcmd = L"PLAY 1-1 DV FILTER yadif=1:-1 LOOP";
144 else if(wcmd.substr(0, 1) == L"5")
146 auto file = wcmd.substr(2, wcmd.length()-1);
147 wcmd = L"PLAY 1-1 " + file + L" LOOP\r\n"
148 L"PLAY 1-2 " + file + L" LOOP\r\n"
149 L"PLAY 1-3 " + file + L" LOOP\r\n"
150 L"PLAY 2-1 " + file + L" LOOP\r\n"
151 L"PLAY 2-2 " + file + L" LOOP\r\n"
152 L"PLAY 2-3 " + file + L" LOOP\r\n";
154 else if(wcmd.substr(0, 1) == L"7")
157 wcmd += L"CLEAR 1\r\n";
158 wcmd += L"MIXER 1 CLEAR\r\n";
159 wcmd += L"PLAY 1-0 GREEN\r\n";
160 wcmd += L"PLAY 1-1 BLUE\r\n";
161 wcmd += L"CG 1-2 ADD 1 ECS_TEST 1\r\n";
162 wcmd += L"MIXER 1-2 FILL 0 -1 1 2\r\n";
164 else if(wcmd.substr(0, 1) == L"8")
167 wcmd += L"MIXER 1-1 FILL 0.0 0.5 1.0 1.0 500 linear DEFER\r\n";
168 wcmd += L"MIXER 1-2 FILL 0.0 0.0 1.0 1.0 500 linear DEFER\r\n";
169 wcmd += L"MIXER 1 COMMIT\r\n";
171 else if(wcmd.substr(0, 1) == L"X")
177 num = boost::lexical_cast<int>(wcmd.substr(1, 2));
178 file = wcmd.substr(4, wcmd.length()-1);
182 num = boost::lexical_cast<int>(wcmd.substr(1, 1));
183 file = wcmd.substr(3, wcmd.length()-1);
194 wcmd = L"MIXER 1 GRID " + boost::lexical_cast<std::wstring>(n);
196 for(int i = 1; i <= num; ++i)
197 wcmd += L"\r\nPLAY 1-" + boost::lexical_cast<std::wstring>(i) + L" " + file + L" LOOP";// + L" SLIDE 100 LOOP";
202 CASPAR_LOG_CURRENT_EXCEPTION();
207 auto strong = amcp.lock();
215 bool run(const std::wstring& config_file_name, tbb::atomic<bool>& should_wait_for_keypress)
217 std::promise<bool> shutdown_server_now;
218 std::future<bool> shutdown_server = shutdown_server_now.get_future();
222 // Create server object which initializes channels, protocols and controllers.
223 std::unique_ptr<server> caspar_server(new server(shutdown_server_now));
225 // For example CEF resets the global locale, so this is to reset it back to "our" preference.
226 setup_global_locale();
228 // Print environment information.
229 print_system_info(caspar_server->get_system_info_provider_repo());
231 std::wstringstream str;
232 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
233 boost::property_tree::write_xml(str, env::properties(), w);
234 CASPAR_LOG(info) << config_file_name << L":\n-----------------------------------------\n" << str.str() << L"-----------------------------------------";
237 CASPAR_SCOPED_CONTEXT_MSG(config_file_name + L": ")
238 caspar_server->start();
241 // Create a dummy client which prints amcp responses to console.
242 auto console_client = spl::make_shared<IO::ConsoleClientInfo>();
244 // Create a amcp parser for console commands.
245 std::shared_ptr<IO::protocol_strategy<wchar_t>> amcp = spl::make_shared<caspar::IO::delimiter_based_chunking_strategy_factory<wchar_t>>(
247 spl::make_shared<caspar::IO::legacy_strategy_adapter_factory>(
248 spl::make_shared<protocol::amcp::AMCPProtocolStrategy>(
250 caspar_server->get_amcp_command_repository())))->create(console_client);
251 std::weak_ptr<IO::protocol_strategy<wchar_t>> weak_amcp = amcp;
253 // Use separate thread for the blocking console input, will be terminated
254 // anyway when the main thread terminates.
255 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...
256 stdin_thread.detach();
257 bool should_restart = shutdown_server.get();
260 while (weak_amcp.lock());
262 return should_restart;
267 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("abort called"));
270 int main(int argc, char** argv)
272 if (intercept_command_line_args(argc, argv))
276 setup_prerequisites();
277 //std::signal(SIGABRT, on_abort);
279 setup_global_locale();
281 std::wcout << L"Type \"q\" to close application." << std::endl;
284 auto debugging_environment = setup_debugging_environment();
286 // Increase process priotity.
287 increase_process_priority();
289 // Install general protection fault handler.
290 ensure_gpf_handler_installed_for_thread("main thread");
292 // Install GPF handler into all tbb threads.
293 struct tbb_thread_installer : public tbb::task_scheduler_observer
295 tbb_thread_installer(){observe(true);}
296 void on_scheduler_entry(bool is_worker) override
298 ensure_gpf_handler_installed_for_thread("tbb-worker-thread");
300 } tbb_thread_installer;
302 tbb::task_scheduler_init init;
303 std::wstring config_file_name(L"casparcg.config");
307 // Configure environment properties from configuration.
309 config_file_name = caspar::u16(argv[1]);
311 env::configure(config_file_name);
313 log::set_log_level(env::properties().get(L"configuration.log-level", L"info"));
314 auto log_categories_str = env::properties().get(L"configuration.log-categories", L"communication");
315 std::set<std::wstring> log_categories;
316 boost::split(log_categories, log_categories_str, boost::is_any_of(L", "));
317 for (auto& log_category : { L"calltrace", L"communication" })
318 log::set_log_category(log_category, log_categories.find(log_category) != log_categories.end());
320 if (env::properties().get(L"configuration.debugging.remote", false))
321 wait_for_remote_debugging();
323 // Start logging to file.
324 log::add_file_sink(env::log_folder() + L"caspar", caspar::log::category != caspar::log::log_category::calltrace);
325 log::add_file_sink(env::log_folder() + L"calltrace", caspar::log::category == caspar::log::log_category::calltrace);
326 std::wcout << L"Logging [info] or higher severity to " << env::log_folder() << std::endl << std::endl;
328 // Once logging to file, log configuration warnings.
329 env::log_configuration_warnings();
331 // Setup console window.
332 setup_console_window();
334 tbb::atomic<bool> should_wait_for_keypress;
335 should_wait_for_keypress = false;
336 auto should_restart = run(config_file_name, should_wait_for_keypress);
337 return_code = should_restart ? 5 : 0;
339 for (auto& thread : get_thread_infos())
341 if (thread->name != "main thread" && thread->name != "tbb-worker-thread")
342 CASPAR_LOG(warning) << L"Thread left running: " << thread->name << L" (" << thread->native_id << L")";
345 CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";
347 if (should_wait_for_keypress)
350 catch(const boost::property_tree::file_parser_error& e)
352 CASPAR_LOG(fatal) << "At " << u8(config_file_name) << ":" << e.line() << ": " << e.message() << ". Please check the configuration file (" << u8(config_file_name) << ") for errors.";
355 catch (const user_error& e)
357 CASPAR_LOG(fatal) << get_message_and_context(e) << " Please check the configuration file (" << u8(config_file_name) << ") for errors.";
362 CASPAR_LOG_CURRENT_EXCEPTION();
363 CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";
364 boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
365 std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";
366 boost::this_thread::sleep_for(boost::chrono::milliseconds(4000));