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 #include <tbb/tbbmalloc_proxy.h>
40 #include "platform_specific.h"
41 #include "included_modules.h"
43 #include <protocol/util/strategy_adapters.h>
44 #include <protocol/amcp/AMCPProtocolStrategy.h>
46 #include <common/env.h>
47 #include <common/except.h>
48 #include <common/log.h>
49 #include <common/gl/gl_check.h>
50 #include <common/os/system_info.h>
51 #include <common/os/general_protection_fault.h>
53 #include <core/system_info_provider.h>
55 #include <boost/property_tree/detail/file_parser_error.hpp>
56 #include <boost/property_tree/xml_parser.hpp>
57 #include <boost/locale.hpp>
58 #include <boost/lexical_cast.hpp>
59 #include <boost/algorithm/string/predicate.hpp>
60 #include <boost/thread.hpp>
61 #include <boost/thread/future.hpp>
62 #include <boost/algorithm/string/case_conv.hpp>
68 using namespace caspar;
70 void setup_global_locale()
72 boost::locale::generator gen;
73 gen.categories(boost::locale::codepage_facet);
75 std::locale::global(gen(""));
80 CASPAR_LOG(info) << L"############################################################################";
81 CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";
82 CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";
83 CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";
84 CASPAR_LOG(info) << L"http://www.casparcg.com/";
85 CASPAR_LOG(info) << L"############################################################################";
86 CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();
87 CASPAR_LOG(info) << L"on " << os_description();
88 CASPAR_LOG(info) << cpu_info();
89 CASPAR_LOG(info) << system_product_name();
92 void print_child(const std::wstring& indent, const std::wstring& elem, const boost::property_tree::wptree& tree)
94 auto data = tree.data();
97 CASPAR_LOG(info) << indent << elem;
99 CASPAR_LOG(info) << indent << elem << L" " << tree.data();
101 for (auto& child : tree)
102 print_child(indent + L" ", child.first, child.second);
105 void print_system_info(const spl::shared_ptr<core::system_info_provider_repository>& repo)
107 boost::property_tree::wptree info;
108 repo->fill_information(info);
110 for (auto& elem : info.get_child(L"system"))
111 print_child(L"", elem.first, elem.second);
114 void do_run(server& caspar_server, std::promise<bool>& shutdown_server_now)
116 // Create a dummy client which prints amcp responses to console.
117 auto console_client = spl::make_shared<IO::ConsoleClientInfo>();
119 // Create a amcp parser for console commands.
120 auto amcp = spl::make_shared<caspar::IO::delimiter_based_chunking_strategy_factory<wchar_t>>(
122 spl::make_shared<caspar::IO::legacy_strategy_adapter_factory>(
123 spl::make_shared<protocol::amcp::AMCPProtocolStrategy>(
124 caspar_server.channels(),
125 caspar_server.get_thumbnail_generator(),
126 caspar_server.get_media_info_repo(),
127 caspar_server.get_system_info_provider_repo(),
128 caspar_server.get_cg_registry(),
129 shutdown_server_now)))->create(console_client);
134 std::getline(std::wcin, wcmd); // TODO: It's blocking...
136 //boost::to_upper(wcmd);
138 if(boost::iequals(wcmd, L"EXIT") || boost::iequals(wcmd, L"Q") || boost::iequals(wcmd, L"QUIT") || boost::iequals(wcmd, L"BYE"))
140 shutdown_server_now.set_value(true); //true to wait for keypress
146 // This is just dummy code for testing.
147 if(wcmd.substr(0, 1) == L"1")
148 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP \r\nPLAY 1-1";
149 else if(wcmd.substr(0, 1) == L"2")
150 wcmd = L"MIXER 1-0 VIDEO IS_KEY 1";
151 else if(wcmd.substr(0, 1) == L"3")
152 wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";
153 else if(wcmd.substr(0, 1) == L"4")
154 wcmd = L"PLAY 1-1 DV FILTER yadif=1:-1 LOOP";
155 else if(wcmd.substr(0, 1) == L"5")
157 auto file = wcmd.substr(2, wcmd.length()-1);
158 wcmd = L"PLAY 1-1 " + file + L" LOOP\r\n"
159 L"PLAY 1-2 " + file + L" LOOP\r\n"
160 L"PLAY 1-3 " + file + L" LOOP\r\n"
161 L"PLAY 2-1 " + file + L" LOOP\r\n"
162 L"PLAY 2-2 " + file + L" LOOP\r\n"
163 L"PLAY 2-3 " + file + L" LOOP\r\n";
165 else if(wcmd.substr(0, 1) == L"7")
168 wcmd += L"CLEAR 1\r\n";
169 wcmd += L"MIXER 1 CLEAR\r\n";
170 wcmd += L"PLAY 1-0 GREEN\r\n";
171 wcmd += L"PLAY 1-1 BLUE\r\n";
172 wcmd += L"CG 1-2 ADD 1 ECS_TEST 1\r\n";
173 wcmd += L"MIXER 1-2 FILL 0 -1 1 2\r\n";
175 else if(wcmd.substr(0, 1) == L"8")
178 wcmd += L"MIXER 1-1 FILL 0.0 0.5 1.0 1.0 500 linear DEFER\r\n";
179 wcmd += L"MIXER 1-2 FILL 0.0 0.0 1.0 1.0 500 linear DEFER\r\n";
180 wcmd += L"MIXER 1 COMMIT\r\n";
182 else if(wcmd.substr(0, 1) == L"X")
188 num = boost::lexical_cast<int>(wcmd.substr(1, 2));
189 file = wcmd.substr(4, wcmd.length()-1);
193 num = boost::lexical_cast<int>(wcmd.substr(1, 1));
194 file = wcmd.substr(3, wcmd.length()-1);
205 wcmd = L"MIXER 1 GRID " + boost::lexical_cast<std::wstring>(n);
207 for(int i = 1; i <= num; ++i)
208 wcmd += L"\r\nPLAY 1-" + boost::lexical_cast<std::wstring>(i) + L" " + file + L" LOOP";// + L" SLIDE 100 LOOP";
213 CASPAR_LOG_CURRENT_EXCEPTION();
224 std::promise<bool> shutdown_server_now;
225 std::future<bool> shutdown_server = shutdown_server_now.get_future();
229 // Create server object which initializes channels, protocols and controllers.
230 server caspar_server(shutdown_server_now);
232 // Print environment information.
233 print_system_info(caspar_server.get_system_info_provider_repo());
235 std::wstringstream str;
236 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
237 boost::property_tree::write_xml(str, env::properties(), w);
238 CASPAR_LOG(info) << L"casparcg.config:\n-----------------------------------------\n" << str.str().c_str() << L"-----------------------------------------";
240 caspar_server.start();
242 //auto console_obs = reactive::make_observer([](const monitor::event& e)
244 // std::stringstream str;
246 // CASPAR_LOG(trace) << str.str().c_str();
249 //caspar_server.subscribe(console_obs);
252 // Use separate thread for the blocking console input, will be terminated
253 // anyway when the main thread terminates.
254 boost::thread stdin_thread(std::bind(do_run, std::ref(caspar_server), std::ref(shutdown_server_now))); //compiler didn't like lambda here...
255 stdin_thread.detach();
256 return shutdown_server.get();
261 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("abort called"));
264 int main(int argc, char** argv)
266 if (intercept_command_line_args(argc, argv))
270 setup_prerequisites();
271 //std::signal(SIGABRT, on_abort);
273 setup_global_locale();
275 std::wcout << L"Type \"q\" to close application." << std::endl;
278 auto debugging_environment = setup_debugging_environment();
280 // Increase process priotity.
281 increase_process_priority();
283 // Install general protection fault handler.
284 ensure_gpf_handler_installed_for_thread("main thread");
286 // Install GPF handler into all tbb threads.
287 struct tbb_thread_installer : public tbb::task_scheduler_observer
289 tbb_thread_installer(){observe(true);}
290 void on_scheduler_entry(bool is_worker)
292 ensure_gpf_handler_installed_for_thread("tbb-worker-thread");
294 } tbb_thread_installer;
296 tbb::task_scheduler_init init;
300 // Configure environment properties from configuration.
301 env::configure(L"casparcg.config");
303 log::set_log_level(env::properties().get(L"configuration.log-level", L"debug"));
305 if (env::properties().get(L"configuration.debugging.remote", false))
306 wait_for_remote_debugging();
308 // Start logging to file.
309 log::add_file_sink(env::log_folder());
310 std::wcout << L"Logging [info] or higher severity to " << env::log_folder() << std::endl << std::endl;
312 // Setup console window.
313 setup_console_window();
315 return_code = run() ? 5 : 0;
317 boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
318 CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";
320 catch(boost::property_tree::file_parser_error&)
322 CASPAR_LOG_CURRENT_EXCEPTION();
323 CASPAR_LOG(fatal) << L"Unhandled configuration error in main thread. Please check the configuration file (casparcg.config) for errors.";
328 CASPAR_LOG_CURRENT_EXCEPTION();
329 CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";
330 boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
331 std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";
332 boost::this_thread::sleep_for(boost::chrono::milliseconds(4000));