2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "compose.hpp"
23 #include "exceptions.h"
24 #include <boost/algorithm/string.hpp>
25 #ifdef DCPOMATIC_LINUX
29 #ifdef DCPOMATIC_WINDOWS
35 #include <sys/sysctl.h>
36 #include <mach-o/dyld.h>
37 #include <IOKit/pwr_mgt/IOPMLib.h>
39 #ifdef DCPOMATIC_POSIX
40 #include <sys/types.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
49 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
50 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
51 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, Log::TYPE_ERROR);
59 using boost::shared_ptr;
61 /** @param s Number of seconds to sleep for */
63 dcpomatic_sleep (int s)
65 #ifdef DCPOMATIC_POSIX
68 #ifdef DCPOMATIC_WINDOWS
73 /** @return A string of CPU information (model name etc.) */
79 #ifdef DCPOMATIC_LINUX
80 /* This use of ifstream is ok; the filename can never
83 ifstream f ("/proc/cpuinfo");
87 if (boost::algorithm::starts_with (l, "model name")) {
88 string::size_type const c = l.find (':');
89 if (c != string::npos) {
90 info = l.substr (c + 2);
98 size_t N = sizeof (buffer);
99 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
104 #ifdef DCPOMATIC_WINDOWS
106 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
112 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
116 if (type != REG_SZ) {
120 wstring value (data / sizeof (wchar_t), L'\0');
121 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
126 info = string (value.begin(), value.end());
136 /** @return Path of the Contents directory in the .app */
137 boost::filesystem::path
140 uint32_t size = 1024;
142 if (_NSGetExecutablePath (buffer, &size)) {
143 throw StringError ("_NSGetExecutablePath failed");
146 boost::filesystem::path path (buffer);
147 path = boost::filesystem::canonical (path);
148 path = path.parent_path ();
149 path = path.parent_path ();
154 boost::filesystem::path
157 #ifdef DCPOMATIC_LINUX
158 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
160 #ifdef DCPOMATIC_WINDOWS
162 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
163 PathRemoveFileSpec (dir);
164 boost::filesystem::path path = dir;
165 return path.parent_path();
168 return app_contents();
173 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
175 #ifdef DCPOMATIC_WINDOWS
176 SECURITY_ATTRIBUTES security;
177 security.nLength = sizeof (security);
178 security.bInheritHandle = TRUE;
179 security.lpSecurityDescriptor = 0;
181 HANDLE child_stderr_read;
182 HANDLE child_stderr_write;
183 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
184 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
189 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
190 PathRemoveFileSpec (dir);
191 SetCurrentDirectory (dir);
193 STARTUPINFO startup_info;
194 ZeroMemory (&startup_info, sizeof (startup_info));
195 startup_info.cb = sizeof (startup_info);
196 startup_info.hStdError = child_stderr_write;
197 startup_info.dwFlags |= STARTF_USESTDHANDLES;
199 wchar_t command[512];
200 wcscpy (command, L"ffprobe.exe \"");
203 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
204 wcscat (command, file);
206 wcscat (command, L"\"");
208 PROCESS_INFORMATION process_info;
209 ZeroMemory (&process_info, sizeof (process_info));
210 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
211 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
215 FILE* o = fopen_boost (out, "w");
217 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
221 CloseHandle (child_stderr_write);
226 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
229 fwrite (buffer, read, 1, o);
234 WaitForSingleObject (process_info.hProcess, INFINITE);
235 CloseHandle (process_info.hProcess);
236 CloseHandle (process_info.hThread);
237 CloseHandle (child_stderr_read);
240 #ifdef DCPOMATIC_LINUX
241 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
242 LOG_GENERAL (N_("Probing with %1"), ffprobe);
243 system (ffprobe.c_str ());
247 boost::filesystem::path path = app_contents();
251 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
252 LOG_GENERAL (N_("Probing with %1"), ffprobe);
253 system (ffprobe.c_str ());
257 list<pair<string, string> >
260 list<pair<string, string> > m;
262 #ifdef DCPOMATIC_LINUX
263 FILE* f = setmntent ("/etc/mtab", "r");
269 struct mntent* mnt = getmntent (f);
274 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
283 boost::filesystem::path
286 #ifdef DCPOMATIC_WINDOWS
288 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
289 PathRemoveFileSpec (dir);
291 boost::filesystem::path path = dir;
292 path /= "openssl.exe";
295 /* We assume that it's on the path for Linux and OS X */
301 /* Apparently there is no way to create an ofstream using a UTF-8
302 filename under Windows. We are hence reduced to using fopen
306 fopen_boost (boost::filesystem::path p, string t)
308 #ifdef DCPOMATIC_WINDOWS
309 wstring w (t.begin(), t.end());
310 /* c_str() here should give a UTF-16 string */
311 return _wfopen (p.c_str(), w.c_str ());
313 return fopen (p.c_str(), t.c_str ());
318 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
320 #ifdef DCPOMATIC_WINDOWS
321 return _fseeki64 (stream, offset, whence);
323 return fseek (stream, offset, whence);
330 #ifdef DCPOMATIC_WINDOWS
331 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
338 /* We should use this */
339 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
340 /* but it's not available on 10.5, so we use this */
341 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
348 IOPMAssertionRelease (_assertion_id);