]> git.sesse.net Git - casparcg/blob - shell/main.cpp
2.0.0.2: key_rect - Fixed problem where fill_rect was modified.
[casparcg] / shell / main.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include <Windows.h>\r
22 \r
23 #include <tbb/tbbmalloc_proxy.h>\r
24 #include <tbb/task_scheduler_observer.h>\r
25 \r
26 #ifdef _DEBUG\r
27         #define _CRTDBG_MAP_ALLOC\r
28         #include <stdlib.h>\r
29         #include <crtdbg.h>\r
30 #endif\r
31 \r
32 #include <conio.h>\r
33 \r
34 #include "bootstrapper.h"\r
35 \r
36 #include <core/producer/flash/flash_producer.h>\r
37 #include <core/producer/flash/cg_producer.h>\r
38 #include <core/producer/image/image_producer.h>\r
39 #include <core/consumer/decklink/decklink_consumer.h>\r
40 #include <core/consumer/bluefish/bluefish_consumer.h>\r
41 \r
42 #include <common/exception/win32_exception.h>\r
43 #include <common/exception/exceptions.h>\r
44 #include <common/log/log.h>\r
45 #include <common/env.h>\r
46 #include <common/utility/assert.h>\r
47 \r
48 #include <mixer/gpu/ogl_device.h>\r
49 \r
50 #include <protocol/amcp/AMCPProtocolStrategy.h>\r
51 \r
52 #include <GLee.h>\r
53 \r
54 #include <boost/foreach.hpp>\r
55 \r
56 #if defined(_MSC_VER)\r
57 #pragma warning (disable : 4244)\r
58 #endif\r
59 \r
60 extern "C" \r
61 {\r
62         #define __STDC_CONSTANT_MACROS\r
63         #define __STDC_LIMIT_MACROS\r
64         #include <libavformat/avformat.h>\r
65         #include <libswscale/swscale.h>\r
66         #include <libavcodec/avcodec.h>\r
67 }\r
68 \r
69 #include <atlbase.h>\r
70 \r
71 using namespace caspar;\r
72 using namespace caspar::core;\r
73 using namespace caspar::protocol;\r
74 \r
75 // NOTE: This is needed in order to make CComObject work since this is not a real ATL project.\r
76 CComModule _AtlModule;\r
77 extern __declspec(selectany) CAtlModule* _pAtlModule = &_AtlModule;\r
78 \r
79 class win32_handler_tbb_installer : public tbb::task_scheduler_observer\r
80 {\r
81 public:\r
82         win32_handler_tbb_installer()   {observe(true);}\r
83         void on_scheduler_entry(bool is_worker) \r
84         {\r
85                 //CASPAR_LOG(debug) << L"Started TBB Worker Thread.";\r
86                 win32_exception::install_handler();\r
87         }\r
88         void on_scheduler_exit()\r
89         {\r
90                 //CASPAR_LOG(debug) << L"Stopped TBB Worker Thread.";\r
91         }\r
92 };\r
93 \r
94 void setup_console_window()\r
95 {       \r
96         auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);\r
97 \r
98         EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);\r
99         DrawMenuBar(GetConsoleWindow());\r
100 \r
101         auto coord = GetLargestConsoleWindowSize(hOut);\r
102         coord.X /= 2;\r
103 \r
104         SetConsoleScreenBufferSize(hOut, coord);\r
105 \r
106         SMALL_RECT DisplayArea = {0, 0, 0, 0};\r
107         DisplayArea.Right = coord.X-1;\r
108         DisplayArea.Bottom = coord.Y-1;\r
109         SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);\r
110                 \r
111         std::wstringstream str;\r
112         str << "CasparCG Server " << env::version();\r
113         SetConsoleTitle(str.str().c_str());\r
114 }\r
115 \r
116 void print_version()\r
117 {       \r
118         try\r
119         {\r
120                 CASPAR_LOG(info) << L"Copyright (c) 2010 Sveriges Television AB, www.casparcg.com, <info@casparcg.com>";\r
121                 CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();\r
122                 CASPAR_LOG(info) << L"Flash " << flash::get_flash_version();\r
123                 CASPAR_LOG(info) << L"Flash-Template-Host " << flash::get_cg_version();\r
124                 CASPAR_LOG(info) << L"FreeImage " << image::get_image_version();\r
125         \r
126                 std::wstring decklink_devices;\r
127                 BOOST_FOREACH(auto& device, get_decklink_device_list())\r
128                         decklink_devices += L"\t" + device;\r
129                 CASPAR_LOG(info) << L"Decklink " << get_decklink_version() << (decklink_devices.empty() ? L"" : L"\n\tDevices:\n" + decklink_devices);\r
130         \r
131                 std::wstring bluefish_devices;\r
132                 BOOST_FOREACH(auto& device, get_bluefish_device_list())\r
133                         bluefish_devices += L"\t" + device;\r
134                 CASPAR_LOG(info) << L"Bluefish " << get_bluefish_version() << (bluefish_devices.empty() ? L"" : L"\n\tDevices:\n" + bluefish_devices);\r
135 \r
136                 CASPAR_LOG(info) << L"FFMPEG-avcodec " << ((avcodec_version() >> 16) & 0xFF) << L"." << ((avcodec_version() >> 8) & 0xFF) << L"." << ((avcodec_version() >> 0) & 0xFF);\r
137                 CASPAR_LOG(info) << L"FFMPEG-swscale " << ((avformat_version() >> 16) & 0xFF) << L"." << ((avformat_version() >> 8) & 0xFF) << L"." << ((avformat_version() >> 0) & 0xFF);\r
138                 CASPAR_LOG(info) << L"FFMPEG-avformat " << ((swscale_version() >> 16) & 0xFF) << L"." << ((swscale_version() >> 8) & 0xFF) << L"." << ((swscale_version() >> 0) & 0xFF);\r
139                 CASPAR_LOG(info) << L"OpenGL " << ogl_device::create()->invoke([]{return reinterpret_cast<const char*>(glGetString(GL_VERSION));})\r
140                                                  << L" "           << ogl_device::create()->invoke([]{return reinterpret_cast<const char*>(glGetString(GL_VENDOR));});\r
141 \r
142                 HKEY hkey; \r
143                 DWORD dwType, dwSize;\r
144                 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)\r
145                 {\r
146                         wchar_t p_name_str[1024];\r
147                         wchar_t csd_ver_str[1024];\r
148                         wchar_t csd_build_str[1024];\r
149 \r
150                         dwType = REG_SZ;\r
151                         dwSize = sizeof(p_name_str);\r
152 \r
153                         RegQueryValueEx(hkey, TEXT("ProductName"), NULL, &dwType, (PBYTE)&p_name_str, &dwSize);\r
154                         RegQueryValueEx(hkey, TEXT("CSDVersion"), NULL, &dwType, (PBYTE)&csd_ver_str, &dwSize);\r
155                         RegQueryValueEx(hkey, TEXT("CSDBuildNumber"), NULL, &dwType, (PBYTE)&csd_build_str, &dwSize);\r
156  \r
157                         RegCloseKey(hkey);\r
158 \r
159                         CASPAR_LOG(info) << p_name_str << L" " << csd_ver_str << L"." << csd_build_str << L"\n";\r
160                 }\r
161         }\r
162         catch(...)\r
163         {\r
164                 CASPAR_LOG(warning) << L"Unexpected exception while printing server information.";\r
165         }\r
166 }\r
167  \r
168 int main(int argc, wchar_t* argv[])\r
169 {       \r
170         try \r
171         {\r
172                 win32_handler_tbb_installer win32_handler_tbb_installer;\r
173                 win32_exception::install_handler();\r
174 \r
175                 env::initialize("caspar.config");\r
176 \r
177                 // Init FFMPEG\r
178                 av_register_all();\r
179                 avcodec_init();\r
180 \r
181                 // Increase time precision\r
182                 timeBeginPeriod(1);\r
183 \r
184                 // Start caspar\r
185 \r
186                 setup_console_window();\r
187 \r
188         #ifdef _DEBUG\r
189                 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF );\r
190                 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );\r
191                 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG );\r
192                 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_DEBUG );\r
193 \r
194                 MessageBox(nullptr, TEXT("Now is the time to connect for remote debugging..."), TEXT("Debug"), MB_OK | MB_TOPMOST);\r
195         #endif\r
196                                          \r
197                 log::add_file_sink(env::log_folder());\r
198 \r
199                 print_version();\r
200                                 \r
201                 bootstrapper caspar_device;\r
202                                 \r
203                 auto dummy = std::make_shared<IO::ConsoleClientInfo>();\r
204                 amcp::AMCPProtocolStrategy amcp(caspar_device.get_channels());\r
205                 bool is_running = true;\r
206                 while(is_running)\r
207                 {\r
208                         std::wstring wcmd;\r
209                         std::getline(std::wcin, wcmd); // TODO: It's blocking...\r
210                         is_running = wcmd != L"exit" && wcmd != L"q";\r
211                         if(wcmd.substr(0, 2) == L"12")\r
212                         {\r
213                                 wcmd = L"LOADBG 1-1 A LOOP AUTOPLAY\r\n";\r
214                                 amcp.Parse(wcmd.c_str(), wcmd.length(), dummy);\r
215                                 wcmd = L"LOADBG 1-2 DV LOOP AUTOPLAY\r\n";\r
216                                 amcp.Parse(wcmd.c_str(), wcmd.length(), dummy);\r
217                                 wcmd = L"MIXER 1-1 VIDEO FIX_RECT 0.0 0.0 0.5 0.5\r\n";\r
218                                 amcp.Parse(wcmd.c_str(), wcmd.length(), dummy);\r
219                                 wcmd = L"MIXER 1-2 VIDEO FIX_RECT 0.5 0.0 0.5 0.5\r\n";\r
220                                 amcp.Parse(wcmd.c_str(), wcmd.length(), dummy);\r
221                         }\r
222                         else if(wcmd.substr(0, 2) == L"10")\r
223                                 wcmd = L"MIXER 1-1 VIDEO CLIP_RECT 0.4 0.4 0.5 0.5";\r
224                         if(wcmd.substr(0, 2) == L"11")\r
225                                 wcmd = L"MIXER 1-1 VIDEO FIX_RECT 0.4 0.4 0.5 0.5";\r
226                         else if(wcmd.substr(0, 1) == L"1")\r
227                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" SLIDE 100 LOOP AUTOPLAY";\r
228                         else if(wcmd.substr(0, 1) == L"2")\r
229                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" PUSH 100 LOOP AUTOPLAY";\r
230                         else if(wcmd.substr(0, 1) == L"3")\r
231                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" MIX 100 LOOP AUTOPLAY";\r
232                         else if(wcmd.substr(0, 1) == L"4")\r
233                                 wcmd = L"LOADBG 1-1 " + wcmd.substr(1, wcmd.length()-1) + L" WIPE 100 LOOP AUTOPLAY";\r
234                         else if(wcmd.substr(0, 1) == L"5")\r
235                                 wcmd = L"LOADBG 1-2 " + wcmd.substr(1, wcmd.length()-1) + L" LOOP AUTOPLAY";\r
236                         else if(wcmd.substr(0, 1) == L"6")\r
237                                 wcmd = L"CG 1-2 ADD 1 BBTELEFONARE 1";\r
238                         else if(wcmd.substr(0, 1) == L"7")\r
239                                 wcmd = L"LOAD 1-1 720p2500";\r
240                         else if(wcmd.substr(0, 1) == L"8")\r
241                                 wcmd = L"LOAD 1-1 #FFFFFFFF AUTOPLAY";\r
242                         else if(wcmd.substr(0, 1) == L"9")\r
243                                 wcmd = L"LOADBG 1-2 " + wcmd.substr(1, wcmd.length()-1) + L" [1.0-2.0] LOOP AUTOPLAY";\r
244 \r
245                         wcmd += L"\r\n";\r
246                         amcp.Parse(wcmd.c_str(), wcmd.length(), dummy);\r
247                 }\r
248         }\r
249         catch(const std::exception&)\r
250         {\r
251                 CASPAR_LOG(fatal) << "UNHANDLED EXCEPTION in main thread.";\r
252                 CASPAR_LOG_CURRENT_EXCEPTION();\r
253                 std::wcout << L"Press Any Key To Exit\n";\r
254                 _getwch();\r
255         }       \r
256 \r
257         timeEndPeriod(1);\r
258 \r
259         CASPAR_LOG(info) << "Successfully shutdown CasparCG Server";\r
260         \r
261         return 0;\r
262 }