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"
24 #include "exceptions.h"
25 #include <boost/algorithm/string.hpp>
26 #ifdef DCPOMATIC_LINUX
30 #ifdef DCPOMATIC_WINDOWS
36 #include <sys/sysctl.h>
37 #include <mach-o/dyld.h>
38 #include <IOKit/pwr_mgt/IOPMLib.h>
40 #ifdef DCPOMATIC_POSIX
41 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
50 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
51 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
52 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
60 using std::runtime_error;
61 using boost::shared_ptr;
63 /** @param s Number of seconds to sleep for */
65 dcpomatic_sleep (int s)
67 #ifdef DCPOMATIC_POSIX
70 #ifdef DCPOMATIC_WINDOWS
75 /** @return A string of CPU information (model name etc.) */
81 #ifdef DCPOMATIC_LINUX
82 /* This use of ifstream is ok; the filename can never
85 ifstream f ("/proc/cpuinfo");
89 if (boost::algorithm::starts_with (l, "model name")) {
90 string::size_type const c = l.find (':');
91 if (c != string::npos) {
92 info = l.substr (c + 2);
100 size_t N = sizeof (buffer);
101 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
106 #ifdef DCPOMATIC_WINDOWS
108 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
114 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
118 if (type != REG_SZ) {
122 wstring value (data / sizeof (wchar_t), L'\0');
123 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
128 info = string (value.begin(), value.end());
138 /** @return Path of the Contents directory in the .app */
139 boost::filesystem::path
142 uint32_t size = 1024;
144 if (_NSGetExecutablePath (buffer, &size)) {
145 throw runtime_error ("_NSGetExecutablePath failed");
148 boost::filesystem::path path (buffer);
149 path = boost::filesystem::canonical (path);
150 path = path.parent_path ();
151 path = path.parent_path ();
156 boost::filesystem::path
159 #ifdef DCPOMATIC_LINUX
160 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
164 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
166 #ifdef DCPOMATIC_WINDOWS
168 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
169 PathRemoveFileSpec (dir);
170 boost::filesystem::path path = dir;
171 return path.parent_path();
174 return app_contents() / "Resources";
179 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
181 #ifdef DCPOMATIC_WINDOWS
182 SECURITY_ATTRIBUTES security;
183 security.nLength = sizeof (security);
184 security.bInheritHandle = TRUE;
185 security.lpSecurityDescriptor = 0;
187 HANDLE child_stderr_read;
188 HANDLE child_stderr_write;
189 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
190 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
195 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
196 PathRemoveFileSpec (dir);
197 SetCurrentDirectory (dir);
199 STARTUPINFO startup_info;
200 ZeroMemory (&startup_info, sizeof (startup_info));
201 startup_info.cb = sizeof (startup_info);
202 startup_info.hStdError = child_stderr_write;
203 startup_info.dwFlags |= STARTF_USESTDHANDLES;
205 wchar_t command[512];
206 wcscpy (command, L"ffprobe.exe \"");
209 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
210 wcscat (command, file);
212 wcscat (command, L"\"");
214 PROCESS_INFORMATION process_info;
215 ZeroMemory (&process_info, sizeof (process_info));
216 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
217 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
221 FILE* o = fopen_boost (out, "w");
223 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
227 CloseHandle (child_stderr_write);
232 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
235 fwrite (buffer, read, 1, o);
240 WaitForSingleObject (process_info.hProcess, INFINITE);
241 CloseHandle (process_info.hProcess);
242 CloseHandle (process_info.hThread);
243 CloseHandle (child_stderr_read);
246 #ifdef DCPOMATIC_LINUX
247 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
248 LOG_GENERAL (N_("Probing with %1"), ffprobe);
249 system (ffprobe.c_str ());
253 boost::filesystem::path path = app_contents();
257 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
258 LOG_GENERAL (N_("Probing with %1"), ffprobe);
259 system (ffprobe.c_str ());
263 list<pair<string, string> >
266 list<pair<string, string> > m;
268 #ifdef DCPOMATIC_LINUX
269 FILE* f = setmntent ("/etc/mtab", "r");
275 struct mntent* mnt = getmntent (f);
280 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
289 boost::filesystem::path
292 #ifdef DCPOMATIC_WINDOWS
294 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
295 PathRemoveFileSpec (dir);
297 boost::filesystem::path path = dir;
298 path /= "openssl.exe";
301 /* We assume that it's on the path for Linux and OS X */
307 /* Apparently there is no way to create an ofstream using a UTF-8
308 filename under Windows. We are hence reduced to using fopen
312 fopen_boost (boost::filesystem::path p, string t)
314 #ifdef DCPOMATIC_WINDOWS
315 wstring w (t.begin(), t.end());
316 /* c_str() here should give a UTF-16 string */
317 return _wfopen (p.c_str(), w.c_str ());
319 return fopen (p.c_str(), t.c_str ());
324 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
326 #ifdef DCPOMATIC_WINDOWS
327 return _fseeki64 (stream, offset, whence);
329 return fseek (stream, offset, whence);
336 #ifdef DCPOMATIC_WINDOWS
337 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
344 /* We should use this */
345 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
346 /* but it's not available on 10.5, so we use this */
347 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
354 IOPMAssertionRelease (_assertion_id);
359 start_batch_converter (boost::filesystem::path dcpomatic)
361 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
362 boost::filesystem::path batch = dcpomatic.parent_path() / "dcpomatic2_batch";
366 boost::filesystem::path batch = dcpomatic.parent_path ();
367 batch = batch.parent_path (); // MacOS
368 batch = batch.parent_path (); // Contents
369 batch = batch.parent_path (); // DCP-o-matic.app
370 batch = batch.parent_path (); // Applications
371 batch /= "DCP-o-matic\\ 2\\ Batch\\ Converter.app";
374 batch /= "dcpomatic2_batch";
377 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
380 std::cout << "start " << batch << " from " << dcpomatic << "\n";
381 int const r = system (batch.string().c_str());
382 exit (WEXITSTATUS (r));
386 #ifdef DCPOMATIC_WINDOWS
387 STARTUPINFO startup_info;
388 ZeroMemory (&startup_info, sizeof (startup_info));
389 startup_info.cb = sizeof (startup_info);
391 PROCESS_INFORMATION process_info;
392 ZeroMemory (&process_info, sizeof (process_info));
395 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
396 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
403 #ifdef DCPOMATIC_WINDOWS
404 return (uint64_t) GetCurrentThreadId ();
406 return (uint64_t) pthread_self ();