]> git.sesse.net Git - casparcg/blob - shell/main.cpp
- Implemented real-time state notification using OSC-UDP.
[casparcg] / shell / main.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 // tbbmalloc_proxy: \r
23 // Replace the standard memory allocation routines in Microsoft* C/C++ RTL \r
24 // (malloc/free, global new/delete, etc.) with the TBB memory allocator. \r
25 \r
26 #ifdef _DEBUG\r
27         #define _CRTDBG_MAP_ALLOC\r
28         #include <stdlib.h>\r
29         #include <crtdbg.h>\r
30 #else\r
31         #include <tbb/tbbmalloc_proxy.h>\r
32 #endif\r
33 \r
34 #include "resource.h"\r
35 \r
36 #include "server.h"\r
37 \r
38 #define NOMINMAX\r
39 #define WIN32_LEAN_AND_MEAN\r
40 \r
41 #include <locale>\r
42 \r
43 #include <windows.h>\r
44 #include <winnt.h>\r
45 #include <mmsystem.h>\r
46 #include <atlbase.h>\r
47 \r
48 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
49 #include <protocol/osc/server.h>\r
50 \r
51 #include <modules/bluefish/bluefish.h>\r
52 #include <modules/decklink/decklink.h>\r
53 #include <modules/flash/flash.h>\r
54 #include <modules/ffmpeg/ffmpeg.h>\r
55 #include <modules/image/image.h>\r
56 \r
57 #include <common/env.h>\r
58 #include <common/exception/win32_exception.h>\r
59 #include <common/exception/exceptions.h>\r
60 #include <common/log/log.h>\r
61 #include <common/gl/gl_check.h>\r
62 #include <common/os/windows/current_version.h>\r
63 #include <common/os/windows/system_info.h>\r
64 \r
65 #include <tbb/task_scheduler_init.h>\r
66 #include <tbb/task_scheduler_observer.h>\r
67 \r
68 #include <boost/property_tree/detail/file_parser_error.hpp>\r
69 #include <boost/property_tree/xml_parser.hpp>\r
70 #include <boost/foreach.hpp>\r
71 #include <boost/thread.hpp>\r
72 #include <boost/thread/future.hpp>\r
73 #include <boost/locale.hpp>\r
74 #include <boost/algorithm/string/case_conv.hpp>\r
75 \r
76 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.\r
77 CComModule _AtlModule;\r
78 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;\r
79 \r
80 void change_icon( const HICON hNewIcon )\r
81 {\r
82    auto hMod = ::LoadLibrary(L"Kernel32.dll"); \r
83    typedef DWORD(__stdcall *SCI)(HICON);\r
84    auto pfnSetConsoleIcon = reinterpret_cast<SCI>(::GetProcAddress(hMod, "SetConsoleIcon")); \r
85    pfnSetConsoleIcon(hNewIcon); \r
86    ::FreeLibrary(hMod);\r
87 }\r
88 \r
89 void setup_global_locale()\r
90 {\r
91         boost::locale::generator gen;\r
92         gen.categories(boost::locale::codepage_facet);\r
93 \r
94         std::locale::global(gen(""));\r
95 }\r
96 \r
97 void setup_console_window()\r
98 {        \r
99         auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);\r
100 \r
101         // Disable close button in console to avoid shutdown without cleanup.\r
102         EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);\r
103         DrawMenuBar(GetConsoleWindow());\r
104         //SetConsoleCtrlHandler(HandlerRoutine, true);\r
105 \r
106         // Configure console size and position.\r
107         auto coord = GetLargestConsoleWindowSize(hOut);\r
108         coord.X /= 2;\r
109 \r
110         SetConsoleScreenBufferSize(hOut, coord);\r
111 \r
112         SMALL_RECT DisplayArea = {0, 0, 0, 0};\r
113         DisplayArea.Right = coord.X-1;\r
114         DisplayArea.Bottom = (coord.Y-1)/2;\r
115         SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);\r
116                  \r
117         change_icon(::LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(101)));\r
118 \r
119         // Set console title.\r
120         std::wstringstream str;\r
121         str << "CasparCG Server " << caspar::env::version();\r
122 #ifdef COMPILE_RELEASE\r
123         str << " Release";\r
124 #elif  COMPILE_PROFILE\r
125         str << " Profile";\r
126 #elif  COMPILE_DEVELOP\r
127         str << " Develop";\r
128 #elif  COMPILE_DEBUG\r
129         str << " Debug";\r
130 #endif\r
131         SetConsoleTitle(str.str().c_str());\r
132 }\r
133 \r
134 void print_info()\r
135 {\r
136         CASPAR_LOG(info) << L"############################################################################";\r
137         CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";\r
138         CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";\r
139         CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";\r
140         CASPAR_LOG(info) << L"http://www.casparcg.com/";\r
141         CASPAR_LOG(info) << L"############################################################################";\r
142         CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << caspar::env::version();\r
143         CASPAR_LOG(info) << L"on " << caspar::get_win_product_name() << L" " << caspar::get_win_sp_version();\r
144         CASPAR_LOG(info) << caspar::get_cpu_info();\r
145         CASPAR_LOG(info) << caspar::get_system_product_name();\r
146         \r
147         CASPAR_LOG(info) << L"Decklink " << caspar::decklink::get_version();\r
148         BOOST_FOREACH(auto device, caspar::decklink::get_device_list())\r
149                 CASPAR_LOG(info) << L" - " << device;   \r
150                 \r
151         CASPAR_LOG(info) << L"Bluefish " << caspar::bluefish::get_version();\r
152         BOOST_FOREACH(auto device, caspar::bluefish::get_device_list())\r
153                 CASPAR_LOG(info) << L" - " << device;   \r
154         \r
155         CASPAR_LOG(info) << L"FreeImage "               << caspar::image::get_version();\r
156         CASPAR_LOG(info) << L"FFMPEG-avcodec "  << caspar::ffmpeg::get_avcodec_version();\r
157         CASPAR_LOG(info) << L"FFMPEG-avformat " << caspar::ffmpeg::get_avformat_version();\r
158         CASPAR_LOG(info) << L"FFMPEG-avfilter " << caspar::ffmpeg::get_avfilter_version();\r
159         CASPAR_LOG(info) << L"FFMPEG-avutil "   << caspar::ffmpeg::get_avutil_version();\r
160         CASPAR_LOG(info) << L"FFMPEG-swscale "  << caspar::ffmpeg::get_swscale_version();\r
161         CASPAR_LOG(info) << L"Flash "                   << caspar::flash::get_version();\r
162         CASPAR_LOG(info) << L"Template-Host "   << caspar::flash::get_cg_version();\r
163 }\r
164 \r
165 LONG WINAPI UserUnhandledExceptionFilter(EXCEPTION_POINTERS* info)\r
166 {\r
167         try\r
168         {\r
169                 CASPAR_LOG(fatal) << L"#######################\n UNHANDLED EXCEPTION: \n" \r
170                         << L"Adress:" << info->ExceptionRecord->ExceptionAddress << L"\n"\r
171                         << L"Code:" << info->ExceptionRecord->ExceptionCode << L"\n"\r
172                         << L"Flag:" << info->ExceptionRecord->ExceptionFlags << L"\n"\r
173                         << L"Info:" << info->ExceptionRecord->ExceptionInformation << L"\n"\r
174                         << L"Continuing execution. \n#######################";\r
175         }\r
176         catch(...){}\r
177 \r
178     return EXCEPTION_EXECUTE_HANDLER;\r
179 }\r
180 \r
181 void make_upper_case(std::wstring& str)\r
182 {\r
183         boost::to_upper(str);\r
184 }\r
185 \r
186 int main(int argc, wchar_t* argv[])\r
187 {       \r
188         static_assert(sizeof(void*) == 4, "64-bit code generation is not supported.");\r
189         \r
190         SetUnhandledExceptionFilter(UserUnhandledExceptionFilter);\r
191 \r
192         setup_global_locale();\r
193 \r
194         std::wcout << L"Type \"q\" to close application." << std::endl;\r
195         \r
196         // Set debug mode.\r
197         #ifdef _DEBUG\r
198                 HANDLE hLogFile;\r
199                 hLogFile = CreateFile(L"crt_log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);\r
200                 std::shared_ptr<void> crt_log(nullptr, [](HANDLE h){::CloseHandle(h);});\r
201 \r
202                 _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);\r
203                 _CrtSetReportMode(_CRT_WARN,    _CRTDBG_MODE_FILE);\r
204                 _CrtSetReportFile(_CRT_WARN,    hLogFile);\r
205                 _CrtSetReportMode(_CRT_ERROR,   _CRTDBG_MODE_FILE);\r
206                 _CrtSetReportFile(_CRT_ERROR,   hLogFile);\r
207                 _CrtSetReportMode(_CRT_ASSERT,  _CRTDBG_MODE_FILE);\r
208                 _CrtSetReportFile(_CRT_ASSERT,  hLogFile);\r
209         #endif\r
210 \r
211         // Increase process priotity.\r
212         SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);\r
213 \r
214         // Install structured exception handler.\r
215         caspar::win32_exception::install_handler();\r
216                                 \r
217         // Increase time precision. This will increase accuracy of function like Sleep(1) from 10 ms to 1 ms.\r
218         struct inc_prec\r
219         {\r
220                 inc_prec(){timeBeginPeriod(1);}\r
221                 ~inc_prec(){timeEndPeriod(1);}\r
222         } inc_prec;     \r
223 \r
224         // Install unstructured exception handlers into all tbb threads.\r
225         struct tbb_thread_installer : public tbb::task_scheduler_observer\r
226         {\r
227                 tbb_thread_installer(){observe(true);}\r
228                 void on_scheduler_entry(bool is_worker)\r
229                 {\r
230                         //caspar::detail::SetThreadName(GetCurrentThreadId(), "tbb-worker-thread");\r
231                         caspar::win32_exception::install_handler();\r
232                 }\r
233         } tbb_thread_installer;\r
234 \r
235         tbb::task_scheduler_init init;\r
236         \r
237         try \r
238         {\r
239                 // Configure environment properties from configuration.\r
240                 caspar::env::configure(L"casparcg.config");\r
241                                 \r
242                 caspar::log::set_log_level(caspar::env::properties().get(L"configuration.log-level", L"debug"));\r
243 \r
244         #ifdef _DEBUG\r
245                 if(caspar::env::properties().get(L"configuration.debugging.remote", false))\r
246                         MessageBox(nullptr, TEXT("Now is the time to connect for remote debugging..."), TEXT("Debug"), MB_OK | MB_TOPMOST);\r
247         #endif   \r
248 \r
249                 // Start logging to file.\r
250                 caspar::log::add_file_sink(caspar::env::log_folder());                  \r
251                 std::wcout << L"Logging [info] or higher severity to " << caspar::env::log_folder() << std::endl << std::endl;\r
252                 \r
253                 // Setup console window.\r
254                 setup_console_window();\r
255 \r
256                 // Print environment information.\r
257                 print_info();\r
258                         \r
259                 std::wstringstream str;\r
260                 boost::property_tree::xml_writer_settings<wchar_t> w(' ', 3);\r
261                 boost::property_tree::write_xml(str, caspar::env::properties(), w);\r
262                 CASPAR_LOG(info) << L"casparcg.config:\n-----------------------------------------\n" << str.str().c_str() << L"-----------------------------------------";\r
263                 bool wait_for_keypress;\r
264 \r
265                 {\r
266                         boost::promise<bool> shutdown_server_now;\r
267                         boost::unique_future<bool> shutdown_server = shutdown_server_now.get_future();\r
268 \r
269                         // Create server object which initializes channels, protocols and controllers.\r
270                         caspar::server caspar_server(shutdown_server_now);\r
271                         \r
272 \r
273                         // Use separate thread for the blocking console input, will be terminated \r
274                         // anyway when the main thread terminates.\r
275                         boost::thread stdin_thread([&caspar_server, &shutdown_server_now]\r
276                         {\r
277                                 // Create a amcp parser for console commands.\r
278                                 caspar::protocol::amcp::AMCPProtocolStrategy amcp(\r
279                                                 caspar_server.get_channels(),\r
280                                                 caspar_server.get_thumbnail_generator(),\r
281                                                 shutdown_server_now);\r
282 \r
283                                 // Create a dummy client which prints amcp responses to console.\r
284                                 auto console_client = std::make_shared<caspar::IO::ConsoleClientInfo>();\r
285                                 std::wstring wcmd;\r
286         \r
287                                 while(true)\r
288                                 {\r
289                                         std::getline(std::wcin, wcmd); // TODO: It's blocking...\r
290                                 \r
291                                         //boost::to_upper(wcmd);  // TODO COMPILER crashes on this line, Strange!\r
292                                         make_upper_case(wcmd);\r
293 \r
294                                         if(wcmd == L"EXIT" || wcmd == L"Q" || wcmd == L"QUIT" || wcmd == L"BYE")\r
295                                         {\r
296                                                 shutdown_server_now.set_value(true); // True to wait for keypress\r
297                                                 break;\r
298                                         }\r
299                                 \r
300                                         try\r
301                                         {\r
302                                                 // This is just dummy code for testing.\r
303                                                 if(wcmd.substr(0, 1) == L"1")\r
304                                                         wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP \r\nPLAY 1-1";\r
305                                                 else if(wcmd.substr(0, 1) == L"2")\r
306                                                         wcmd = L"MIXER 1-0 VIDEO IS_KEY 1";\r
307                                                 else if(wcmd.substr(0, 1) == L"3")\r
308                                                         wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";\r
309                                                 else if(wcmd.substr(0, 1) == L"4")\r
310                                                         wcmd = L"PLAY 1-1 DV FILTER yadif=1:-1 LOOP";\r
311                                                 else if(wcmd.substr(0, 1) == L"5")\r
312                                                 {\r
313                                                         auto file = wcmd.substr(2, wcmd.length()-1);\r
314                                                         wcmd = L"PLAY 1-1 " + file + L" LOOP\r\n" \r
315                                                                         L"PLAY 1-2 " + file + L" LOOP\r\n" \r
316                                                                         L"PLAY 1-3 " + file + L" LOOP\r\n"\r
317                                                                         L"PLAY 2-1 " + file + L" LOOP\r\n" \r
318                                                                         L"PLAY 2-2 " + file + L" LOOP\r\n" \r
319                                                                         L"PLAY 2-3 " + file + L" LOOP\r\n";\r
320                                                 }\r
321                                                 else if(wcmd.substr(0, 1) == L"X")\r
322                                                 {\r
323                                                         int num = 0;\r
324                                                         std::wstring file;\r
325                                                         try\r
326                                                         {\r
327                                                                 num = boost::lexical_cast<int>(wcmd.substr(1, 2));\r
328                                                                 file = wcmd.substr(4, wcmd.length()-1);\r
329                                                         }\r
330                                                         catch(...)\r
331                                                         {\r
332                                                                 num = boost::lexical_cast<int>(wcmd.substr(1, 1));\r
333                                                                 file = wcmd.substr(3, wcmd.length()-1);\r
334                                                         }\r
335 \r
336                                                         int n = 0;\r
337                                                         int num2 = num;\r
338                                                         while(num2 > 0)\r
339                                                         {\r
340                                                                 num2 >>= 1;\r
341                                                                 n++;\r
342                                                         }\r
343 \r
344                                                         wcmd = L"MIXER 1 GRID " + boost::lexical_cast<std::wstring>(n);\r
345 \r
346                                                         for(int i = 1; i <= num; ++i)\r
347                                                                 wcmd += L"\r\nPLAY 1-" + boost::lexical_cast<std::wstring>(i) + L" " + file + L" LOOP";// + L" SLIDE 100 LOOP";\r
348                                                 }\r
349                                         }\r
350                                         catch (...)\r
351                                         {\r
352                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
353                                                 continue;\r
354                                         }\r
355 \r
356                                         wcmd += L"\r\n";\r
357                                         amcp.Parse(wcmd.c_str(), wcmd.length(), console_client);\r
358                                 }       \r
359                         });\r
360                         stdin_thread.detach();\r
361                         wait_for_keypress = shutdown_server.get();\r
362                 }\r
363                 Sleep(500);\r
364                 CASPAR_LOG(info) << "Successfully shutdown CasparCG Server.";\r
365 \r
366                 if (wait_for_keypress)\r
367                         system("pause");        \r
368         }\r
369         catch(boost::property_tree::file_parser_error&)\r
370         {\r
371                 CASPAR_LOG_CURRENT_EXCEPTION();\r
372                 CASPAR_LOG(fatal) << L"Unhandled configuration error in main thread. Please check the configuration file (casparcg.config) for errors.";\r
373                 system("pause");        \r
374         }\r
375         catch(...)\r
376         {\r
377                 CASPAR_LOG_CURRENT_EXCEPTION();\r
378                 CASPAR_LOG(fatal) << L"Unhandled exception in main thread. Please report this error on the CasparCG forums (www.casparcg.com/forum).";\r
379                 Sleep(1000);\r
380                 std::wcout << L"\n\nCasparCG will automatically shutdown. See the log file located at the configured log-file folder for more information.\n\n";\r
381                 Sleep(4000);\r
382         }       \r
383         \r
384         return 0;\r
385 }