2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
24 #include "dcpomatic_log.h"
26 #include "exceptions.h"
28 #include <libavformat/avio.h>
30 #include <boost/algorithm/string.hpp>
31 #ifdef DCPOMATIC_LINUX
35 #ifdef DCPOMATIC_WINDOWS
43 #include <sys/sysctl.h>
44 #include <mach-o/dyld.h>
45 #include <IOKit/pwr_mgt/IOPMLib.h>
47 #ifdef DCPOMATIC_POSIX
48 #include <sys/types.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
63 using std::runtime_error;
64 using boost::shared_ptr;
66 /** @param s Number of seconds to sleep for */
68 dcpomatic_sleep_seconds (int s)
70 #ifdef DCPOMATIC_POSIX
73 #ifdef DCPOMATIC_WINDOWS
79 dcpomatic_sleep_milliseconds (int ms)
81 #ifdef DCPOMATIC_POSIX
84 #ifdef DCPOMATIC_WINDOWS
89 /** @return A string of CPU information (model name etc.) */
95 #ifdef DCPOMATIC_LINUX
96 /* This use of ifstream is ok; the filename can never
99 ifstream f ("/proc/cpuinfo");
103 if (boost::algorithm::starts_with (l, "model name")) {
104 string::size_type const c = l.find (':');
105 if (c != string::npos) {
106 info = l.substr (c + 2);
114 size_t N = sizeof (buffer);
115 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
120 #ifdef DCPOMATIC_WINDOWS
122 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
128 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
132 if (type != REG_SZ) {
136 wstring value (data / sizeof (wchar_t), L'\0');
137 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
142 info = string (value.begin(), value.end());
152 /** @return Path of the Contents directory in the .app */
153 boost::filesystem::path
156 uint32_t size = 1024;
158 if (_NSGetExecutablePath (buffer, &size)) {
159 throw runtime_error ("_NSGetExecutablePath failed");
162 boost::filesystem::path path (buffer);
163 path = boost::filesystem::canonical (path);
164 path = path.parent_path ();
165 path = path.parent_path ();
170 boost::filesystem::path
173 #ifdef DCPOMATIC_LINUX
174 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
178 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
180 #ifdef DCPOMATIC_WINDOWS
182 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
183 PathRemoveFileSpec (dir);
184 boost::filesystem::path path = dir;
185 return path.parent_path();
188 return app_contents() / "Resources";
193 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
195 #ifdef DCPOMATIC_WINDOWS
196 SECURITY_ATTRIBUTES security;
197 security.nLength = sizeof (security);
198 security.bInheritHandle = TRUE;
199 security.lpSecurityDescriptor = 0;
201 HANDLE child_stderr_read;
202 HANDLE child_stderr_write;
203 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
204 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
209 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
210 PathRemoveFileSpec (dir);
211 SetCurrentDirectory (dir);
213 STARTUPINFO startup_info;
214 ZeroMemory (&startup_info, sizeof (startup_info));
215 startup_info.cb = sizeof (startup_info);
216 startup_info.hStdError = child_stderr_write;
217 startup_info.dwFlags |= STARTF_USESTDHANDLES;
219 wchar_t command[512];
220 wcscpy (command, L"ffprobe.exe \"");
223 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
224 wcscat (command, file);
226 wcscat (command, L"\"");
228 PROCESS_INFORMATION process_info;
229 ZeroMemory (&process_info, sizeof (process_info));
230 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
231 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
235 FILE* o = fopen_boost (out, "w");
237 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
241 CloseHandle (child_stderr_write);
246 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
249 fwrite (buffer, read, 1, o);
254 WaitForSingleObject (process_info.hProcess, INFINITE);
255 CloseHandle (process_info.hProcess);
256 CloseHandle (process_info.hThread);
257 CloseHandle (child_stderr_read);
260 #ifdef DCPOMATIC_LINUX
261 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
262 LOG_GENERAL (N_("Probing with %1"), ffprobe);
263 system (ffprobe.c_str ());
267 boost::filesystem::path path = app_contents();
271 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
272 LOG_GENERAL (N_("Probing with %1"), ffprobe);
273 system (ffprobe.c_str ());
277 list<pair<string, string> >
280 list<pair<string, string> > m;
282 #ifdef DCPOMATIC_LINUX
283 FILE* f = setmntent ("/etc/mtab", "r");
289 struct mntent* mnt = getmntent (f);
294 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
303 boost::filesystem::path
306 #ifdef DCPOMATIC_WINDOWS
308 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
309 PathRemoveFileSpec (dir);
311 boost::filesystem::path path = dir;
312 path /= "openssl.exe";
317 boost::filesystem::path path = app_contents();
323 #ifdef DCPOMATIC_LINUX
324 return "dcpomatic2_openssl";
329 /* Apparently there is no way to create an ofstream using a UTF-8
330 filename under Windows. We are hence reduced to using fopen
334 fopen_boost (boost::filesystem::path p, string t)
336 #ifdef DCPOMATIC_WINDOWS
337 wstring w (t.begin(), t.end());
338 /* c_str() here should give a UTF-16 string */
339 return _wfopen (p.c_str(), w.c_str ());
341 return fopen (p.c_str(), t.c_str ());
346 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
348 #ifdef DCPOMATIC_WINDOWS
349 return _fseeki64 (stream, offset, whence);
351 return fseek (stream, offset, whence);
358 #ifdef DCPOMATIC_WINDOWS
359 boost::mutex::scoped_lock lm (_mutex);
360 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
367 boost::mutex::scoped_lock lm (_mutex);
368 /* We should use this */
369 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
370 /* but it's not available on 10.5, so we use this */
371 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
378 boost::mutex::scoped_lock lm (_mutex);
379 IOPMAssertionRelease (_assertion_id);
384 start_tool (boost::filesystem::path dcpomatic, string executable,
392 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
393 boost::filesystem::path batch = dcpomatic.parent_path() / executable;
397 boost::filesystem::path batch = dcpomatic.parent_path ();
398 batch = batch.parent_path (); // MacOS
399 batch = batch.parent_path (); // Contents
400 batch = batch.parent_path (); // DCP-o-matic.app
401 batch = batch.parent_path (); // Applications
408 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
411 int const r = system (batch.string().c_str());
412 exit (WEXITSTATUS (r));
416 #ifdef DCPOMATIC_WINDOWS
417 STARTUPINFO startup_info;
418 ZeroMemory (&startup_info, sizeof (startup_info));
419 startup_info.cb = sizeof (startup_info);
421 PROCESS_INFORMATION process_info;
422 ZeroMemory (&process_info, sizeof (process_info));
425 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
426 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
431 start_batch_converter (boost::filesystem::path dcpomatic)
433 start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
437 start_player (boost::filesystem::path dcpomatic)
439 start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
445 #ifdef DCPOMATIC_WINDOWS
446 return (uint64_t) GetCurrentThreadId ();
448 return (uint64_t) pthread_self ();
453 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
455 #ifdef DCPOMATIC_WINDOWS
456 int const length = (file.string().length() + 1) * 2;
457 char* utf8 = new char[length];
458 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
459 int const r = avio_open (s, utf8, flags);
463 return avio_open (s, file.c_str(), flags);
467 #ifdef DCPOMATIC_WINDOWS
469 maybe_open_console ()
471 if (Config::instance()->win32_console ()) {
474 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
475 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
476 FILE* hf_out = _fdopen(hCrt, "w");
477 setvbuf(hf_out, NULL, _IONBF, 1);
480 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
481 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
482 FILE* hf_in = _fdopen(hCrt, "r");
483 setvbuf(hf_in, NULL, _IONBF, 128);
489 boost::filesystem::path
492 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
493 return getenv("HOME");
495 #ifdef DCPOMATIC_WINDOWS
496 return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
501 command_and_read (string cmd)
503 #ifdef DCPOMATIC_LINUX
504 FILE* pipe = popen (cmd.c_str(), "r");
506 throw runtime_error ("popen failed");
512 while (fgets(buffer, sizeof(buffer), pipe)) {
527 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
531 #ifdef DCPOMATIC_WINDOWS
533 IsWow64Process (GetCurrentProcess(), &p);
536 /* XXX: assuming nobody does this on Linux / OS X */