2 Copyright (C) 2012-2015 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"
25 #include "exceptions.h"
27 #include <libavformat/avio.h>
29 #include <boost/algorithm/string.hpp>
30 #ifdef DCPOMATIC_LINUX
34 #ifdef DCPOMATIC_WINDOWS
42 #include <sys/sysctl.h>
43 #include <mach-o/dyld.h>
44 #include <IOKit/pwr_mgt/IOPMLib.h>
46 #ifdef DCPOMATIC_POSIX
47 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
56 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
57 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
58 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
66 using std::runtime_error;
67 using boost::shared_ptr;
69 /** @param s Number of seconds to sleep for */
71 dcpomatic_sleep (int s)
73 #ifdef DCPOMATIC_POSIX
76 #ifdef DCPOMATIC_WINDOWS
81 /** @return A string of CPU information (model name etc.) */
87 #ifdef DCPOMATIC_LINUX
88 /* This use of ifstream is ok; the filename can never
91 ifstream f ("/proc/cpuinfo");
95 if (boost::algorithm::starts_with (l, "model name")) {
96 string::size_type const c = l.find (':');
97 if (c != string::npos) {
98 info = l.substr (c + 2);
106 size_t N = sizeof (buffer);
107 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
112 #ifdef DCPOMATIC_WINDOWS
114 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
120 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
124 if (type != REG_SZ) {
128 wstring value (data / sizeof (wchar_t), L'\0');
129 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
134 info = string (value.begin(), value.end());
144 /** @return Path of the Contents directory in the .app */
145 boost::filesystem::path
148 uint32_t size = 1024;
150 if (_NSGetExecutablePath (buffer, &size)) {
151 throw runtime_error ("_NSGetExecutablePath failed");
154 boost::filesystem::path path (buffer);
155 path = boost::filesystem::canonical (path);
156 path = path.parent_path ();
157 path = path.parent_path ();
162 boost::filesystem::path
165 #ifdef DCPOMATIC_LINUX
166 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
170 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
172 #ifdef DCPOMATIC_WINDOWS
174 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
175 PathRemoveFileSpec (dir);
176 boost::filesystem::path path = dir;
177 return path.parent_path();
180 return app_contents() / "Resources";
185 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
187 #ifdef DCPOMATIC_WINDOWS
188 SECURITY_ATTRIBUTES security;
189 security.nLength = sizeof (security);
190 security.bInheritHandle = TRUE;
191 security.lpSecurityDescriptor = 0;
193 HANDLE child_stderr_read;
194 HANDLE child_stderr_write;
195 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
196 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
201 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
202 PathRemoveFileSpec (dir);
203 SetCurrentDirectory (dir);
205 STARTUPINFO startup_info;
206 ZeroMemory (&startup_info, sizeof (startup_info));
207 startup_info.cb = sizeof (startup_info);
208 startup_info.hStdError = child_stderr_write;
209 startup_info.dwFlags |= STARTF_USESTDHANDLES;
211 wchar_t command[512];
212 wcscpy (command, L"ffprobe.exe \"");
215 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
216 wcscat (command, file);
218 wcscat (command, L"\"");
220 PROCESS_INFORMATION process_info;
221 ZeroMemory (&process_info, sizeof (process_info));
222 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
223 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
227 FILE* o = fopen_boost (out, "w");
229 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
233 CloseHandle (child_stderr_write);
238 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
241 fwrite (buffer, read, 1, o);
246 WaitForSingleObject (process_info.hProcess, INFINITE);
247 CloseHandle (process_info.hProcess);
248 CloseHandle (process_info.hThread);
249 CloseHandle (child_stderr_read);
252 #ifdef DCPOMATIC_LINUX
253 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
254 LOG_GENERAL (N_("Probing with %1"), ffprobe);
255 system (ffprobe.c_str ());
259 boost::filesystem::path path = app_contents();
263 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
264 LOG_GENERAL (N_("Probing with %1"), ffprobe);
265 system (ffprobe.c_str ());
269 list<pair<string, string> >
272 list<pair<string, string> > m;
274 #ifdef DCPOMATIC_LINUX
275 FILE* f = setmntent ("/etc/mtab", "r");
281 struct mntent* mnt = getmntent (f);
286 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
295 boost::filesystem::path
298 #ifdef DCPOMATIC_WINDOWS
300 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
301 PathRemoveFileSpec (dir);
303 boost::filesystem::path path = dir;
304 path /= "openssl.exe";
307 /* We assume that it's on the path for Linux and OS X */
313 /* Apparently there is no way to create an ofstream using a UTF-8
314 filename under Windows. We are hence reduced to using fopen
318 fopen_boost (boost::filesystem::path p, string t)
320 #ifdef DCPOMATIC_WINDOWS
321 wstring w (t.begin(), t.end());
322 /* c_str() here should give a UTF-16 string */
323 return _wfopen (p.c_str(), w.c_str ());
325 return fopen (p.c_str(), t.c_str ());
330 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
332 #ifdef DCPOMATIC_WINDOWS
333 return _fseeki64 (stream, offset, whence);
335 return fseek (stream, offset, whence);
342 #ifdef DCPOMATIC_WINDOWS
343 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
350 /* We should use this */
351 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
352 /* but it's not available on 10.5, so we use this */
353 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
360 IOPMAssertionRelease (_assertion_id);
365 start_batch_converter (boost::filesystem::path dcpomatic)
367 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
368 boost::filesystem::path batch = dcpomatic.parent_path() / "dcpomatic2_batch";
372 boost::filesystem::path batch = dcpomatic.parent_path ();
373 batch = batch.parent_path (); // MacOS
374 batch = batch.parent_path (); // Contents
375 batch = batch.parent_path (); // DCP-o-matic.app
376 batch = batch.parent_path (); // Applications
377 batch /= "DCP-o-matic\\ 2\\ Batch\\ Converter.app";
380 batch /= "dcpomatic2_batch";
383 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
386 int const r = system (batch.string().c_str());
387 exit (WEXITSTATUS (r));
391 #ifdef DCPOMATIC_WINDOWS
392 STARTUPINFO startup_info;
393 ZeroMemory (&startup_info, sizeof (startup_info));
394 startup_info.cb = sizeof (startup_info);
396 PROCESS_INFORMATION process_info;
397 ZeroMemory (&process_info, sizeof (process_info));
400 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
401 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
408 #ifdef DCPOMATIC_WINDOWS
409 return (uint64_t) GetCurrentThreadId ();
411 return (uint64_t) pthread_self ();
416 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
418 #ifdef DCPOMATIC_WINDOWS
419 int const length = (file.string().length() + 1) * 2;
420 char* utf8 = new char[length];
421 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
422 int const r = avio_open (s, utf8, flags);
426 return avio_open (s, file.c_str(), flags);
430 #ifdef DCPOMATIC_WINDOWS
432 maybe_open_console ()
434 if (Config::instance()->win32_console ()) {
437 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
438 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
439 FILE* hf_out = _fdopen(hCrt, "w");
440 setvbuf(hf_out, NULL, _IONBF, 1);
443 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
444 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
445 FILE* hf_in = _fdopen(hCrt, "r");
446 setvbuf(hf_in, NULL, _IONBF, 128);