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"
26 #include <libavformat/avio.h>
28 #include <boost/algorithm/string.hpp>
29 #ifdef DCPOMATIC_LINUX
33 #ifdef DCPOMATIC_WINDOWS
39 #include <sys/sysctl.h>
40 #include <mach-o/dyld.h>
41 #include <IOKit/pwr_mgt/IOPMLib.h>
43 #ifdef DCPOMATIC_POSIX
44 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
53 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
54 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
55 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
63 using std::runtime_error;
64 using boost::shared_ptr;
66 /** @param s Number of seconds to sleep for */
68 dcpomatic_sleep (int s)
70 #ifdef DCPOMATIC_POSIX
73 #ifdef DCPOMATIC_WINDOWS
78 /** @return A string of CPU information (model name etc.) */
84 #ifdef DCPOMATIC_LINUX
85 /* This use of ifstream is ok; the filename can never
88 ifstream f ("/proc/cpuinfo");
92 if (boost::algorithm::starts_with (l, "model name")) {
93 string::size_type const c = l.find (':');
94 if (c != string::npos) {
95 info = l.substr (c + 2);
103 size_t N = sizeof (buffer);
104 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
109 #ifdef DCPOMATIC_WINDOWS
111 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
117 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
121 if (type != REG_SZ) {
125 wstring value (data / sizeof (wchar_t), L'\0');
126 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
131 info = string (value.begin(), value.end());
141 /** @return Path of the Contents directory in the .app */
142 boost::filesystem::path
145 uint32_t size = 1024;
147 if (_NSGetExecutablePath (buffer, &size)) {
148 throw runtime_error ("_NSGetExecutablePath failed");
151 boost::filesystem::path path (buffer);
152 path = boost::filesystem::canonical (path);
153 path = path.parent_path ();
154 path = path.parent_path ();
159 boost::filesystem::path
162 #ifdef DCPOMATIC_LINUX
163 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
167 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
169 #ifdef DCPOMATIC_WINDOWS
171 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
172 PathRemoveFileSpec (dir);
173 boost::filesystem::path path = dir;
174 return path.parent_path();
177 return app_contents() / "Resources";
182 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
184 #ifdef DCPOMATIC_WINDOWS
185 SECURITY_ATTRIBUTES security;
186 security.nLength = sizeof (security);
187 security.bInheritHandle = TRUE;
188 security.lpSecurityDescriptor = 0;
190 HANDLE child_stderr_read;
191 HANDLE child_stderr_write;
192 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
193 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
198 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
199 PathRemoveFileSpec (dir);
200 SetCurrentDirectory (dir);
202 STARTUPINFO startup_info;
203 ZeroMemory (&startup_info, sizeof (startup_info));
204 startup_info.cb = sizeof (startup_info);
205 startup_info.hStdError = child_stderr_write;
206 startup_info.dwFlags |= STARTF_USESTDHANDLES;
208 wchar_t command[512];
209 wcscpy (command, L"ffprobe.exe \"");
212 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
213 wcscat (command, file);
215 wcscat (command, L"\"");
217 PROCESS_INFORMATION process_info;
218 ZeroMemory (&process_info, sizeof (process_info));
219 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
220 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
224 FILE* o = fopen_boost (out, "w");
226 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
230 CloseHandle (child_stderr_write);
235 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
238 fwrite (buffer, read, 1, o);
243 WaitForSingleObject (process_info.hProcess, INFINITE);
244 CloseHandle (process_info.hProcess);
245 CloseHandle (process_info.hThread);
246 CloseHandle (child_stderr_read);
249 #ifdef DCPOMATIC_LINUX
250 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
251 LOG_GENERAL (N_("Probing with %1"), ffprobe);
252 system (ffprobe.c_str ());
256 boost::filesystem::path path = app_contents();
260 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
261 LOG_GENERAL (N_("Probing with %1"), ffprobe);
262 system (ffprobe.c_str ());
266 list<pair<string, string> >
269 list<pair<string, string> > m;
271 #ifdef DCPOMATIC_LINUX
272 FILE* f = setmntent ("/etc/mtab", "r");
278 struct mntent* mnt = getmntent (f);
283 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
292 boost::filesystem::path
295 #ifdef DCPOMATIC_WINDOWS
297 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
298 PathRemoveFileSpec (dir);
300 boost::filesystem::path path = dir;
301 path /= "openssl.exe";
304 /* We assume that it's on the path for Linux and OS X */
310 /* Apparently there is no way to create an ofstream using a UTF-8
311 filename under Windows. We are hence reduced to using fopen
315 fopen_boost (boost::filesystem::path p, string t)
317 #ifdef DCPOMATIC_WINDOWS
318 wstring w (t.begin(), t.end());
319 /* c_str() here should give a UTF-16 string */
320 return _wfopen (p.c_str(), w.c_str ());
322 return fopen (p.c_str(), t.c_str ());
327 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
329 #ifdef DCPOMATIC_WINDOWS
330 return _fseeki64 (stream, offset, whence);
332 return fseek (stream, offset, whence);
339 #ifdef DCPOMATIC_WINDOWS
340 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
347 /* We should use this */
348 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
349 /* but it's not available on 10.5, so we use this */
350 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
357 IOPMAssertionRelease (_assertion_id);
362 start_batch_converter (boost::filesystem::path dcpomatic)
364 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
365 boost::filesystem::path batch = dcpomatic.parent_path() / "dcpomatic2_batch";
369 boost::filesystem::path batch = dcpomatic.parent_path ();
370 batch = batch.parent_path (); // MacOS
371 batch = batch.parent_path (); // Contents
372 batch = batch.parent_path (); // DCP-o-matic.app
373 batch = batch.parent_path (); // Applications
374 batch /= "DCP-o-matic\\ 2\\ Batch\\ Converter.app";
377 batch /= "dcpomatic2_batch";
380 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
383 std::cout << "start " << batch << " from " << dcpomatic << "\n";
384 int const r = system (batch.string().c_str());
385 exit (WEXITSTATUS (r));
389 #ifdef DCPOMATIC_WINDOWS
390 STARTUPINFO startup_info;
391 ZeroMemory (&startup_info, sizeof (startup_info));
392 startup_info.cb = sizeof (startup_info);
394 PROCESS_INFORMATION process_info;
395 ZeroMemory (&process_info, sizeof (process_info));
398 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
399 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
406 #ifdef DCPOMATIC_WINDOWS
407 return (uint64_t) GetCurrentThreadId ();
409 return (uint64_t) pthread_self ();
414 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
416 #ifdef DCPOMATIC_WINDOWS
417 int const length = (file.string().length() + 1) * 2;
418 char* utf8 = new char[length];
419 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
420 int const r = avio_open (s, utf8, flags);
424 return avio_open (s, file.c_str(), flags);