2 Copyright (C) 2012 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 <boost/algorithm/string.hpp>
23 #include "compose.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>
45 #include "exceptions.h"
49 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), Log::GENERAL);
50 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), Log::ERROR);
58 using boost::shared_ptr;
60 /** @param s Number of seconds to sleep for */
62 dcpomatic_sleep (int s)
64 #ifdef DCPOMATIC_POSIX
67 #ifdef DCPOMATIC_WINDOWS
72 /** @return A string of CPU information (model name etc.) */
78 #ifdef DCPOMATIC_LINUX
79 /* This use of ifstream is ok; the filename can never
82 ifstream f ("/proc/cpuinfo");
86 if (boost::algorithm::starts_with (l, "model name")) {
87 string::size_type const c = l.find (':');
88 if (c != string::npos) {
89 info = l.substr (c + 2);
97 size_t N = sizeof (buffer);
98 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
103 #ifdef DCPOMATIC_WINDOWS
105 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
111 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
115 if (type != REG_SZ) {
119 wstring value (data / sizeof (wchar_t), L'\0');
120 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
125 info = string (value.begin(), value.end());
135 /** @return Path of the Contents directory in the .app */
136 boost::filesystem::path
139 uint32_t size = 1024;
141 if (_NSGetExecutablePath (buffer, &size)) {
142 throw StringError ("_NSGetExecutablePath failed");
145 boost::filesystem::path path (buffer);
146 path = boost::filesystem::canonical (path);
147 path = path.parent_path ();
148 path = path.parent_path ();
154 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
156 #ifdef DCPOMATIC_WINDOWS
157 SECURITY_ATTRIBUTES security;
158 security.nLength = sizeof (security);
159 security.bInheritHandle = TRUE;
160 security.lpSecurityDescriptor = 0;
162 HANDLE child_stderr_read;
163 HANDLE child_stderr_write;
164 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
165 log->log ("ffprobe call failed (could not CreatePipe)");
170 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
171 PathRemoveFileSpec (dir);
172 SetCurrentDirectory (dir);
174 STARTUPINFO startup_info;
175 ZeroMemory (&startup_info, sizeof (startup_info));
176 startup_info.cb = sizeof (startup_info);
177 startup_info.hStdError = child_stderr_write;
178 startup_info.dwFlags |= STARTF_USESTDHANDLES;
180 wchar_t command[512];
181 wcscpy (command, L"ffprobe.exe \"");
184 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
185 wcscat (command, file);
187 wcscat (command, L"\"");
189 PROCESS_INFORMATION process_info;
190 ZeroMemory (&process_info, sizeof (process_info));
191 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
192 LOG_ERROR (N_("ffprobe call failed (could not CreateProcess)"));
196 FILE* o = fopen_boost (out, "w");
198 LOG_ERROR (N_("ffprobe call failed (could not create output file)"));
202 CloseHandle (child_stderr_write);
207 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
210 fwrite (buffer, read, 1, o);
215 WaitForSingleObject (process_info.hProcess, INFINITE);
216 CloseHandle (process_info.hProcess);
217 CloseHandle (process_info.hThread);
218 CloseHandle (child_stderr_read);
221 #ifdef DCPOMATIC_LINUX
222 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
223 LOG_GENERAL (N_("Probing with %1"), ffprobe);
224 system (ffprobe.c_str ());
228 boost::filesystem::path path = app_contents();
232 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
233 LOG_GENERAL (N_("Probing with %1"), ffprobe);
234 system (ffprobe.c_str ());
238 list<pair<string, string> >
241 list<pair<string, string> > m;
243 #ifdef DCPOMATIC_LINUX
244 FILE* f = setmntent ("/etc/mtab", "r");
250 struct mntent* mnt = getmntent (f);
255 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
264 boost::filesystem::path
267 #ifdef DCPOMATIC_WINDOWS
269 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
270 PathRemoveFileSpec (dir);
272 boost::filesystem::path path = dir;
273 path /= "openssl.exe";
276 /* We assume that it's on the path for Linux and OS X */
282 /* Apparently there is no way to create an ofstream using a UTF-8
283 filename under Windows. We are hence reduced to using fopen
287 fopen_boost (boost::filesystem::path p, string t)
289 #ifdef DCPOMATIC_WINDOWS
290 wstring w (t.begin(), t.end());
291 /* c_str() here should give a UTF-16 string */
292 return _wfopen (p.c_str(), w.c_str ());
294 return fopen (p.c_str(), t.c_str ());
299 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
301 #ifdef DCPOMATIC_WINDOWS
302 return _fseeki64 (stream, offset, whence);
304 return fseek (stream, offset, whence);
311 #ifdef DCPOMATIC_WINDOWS
312 SetThreadExecutionState (ES_CONTINUOUS);
319 /* We should use this */
320 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
321 /* but it's not available on 10.5, so we use this */
322 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
329 IOPMAssertionRelease (_assertion_id);