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>
38 #ifdef DCPOMATIC_POSIX
39 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include "exceptions.h"
52 using boost::shared_ptr;
54 /** @param s Number of seconds to sleep for */
56 dcpomatic_sleep (int s)
58 #ifdef DCPOMATIC_POSIX
61 #ifdef DCPOMATIC_WINDOWS
66 /** @return A string of CPU information (model name etc.) */
72 #ifdef DCPOMATIC_LINUX
73 ifstream f ("/proc/cpuinfo");
77 if (boost::algorithm::starts_with (l, "model name")) {
78 string::size_type const c = l.find (':');
79 if (c != string::npos) {
80 info = l.substr (c + 2);
88 size_t N = sizeof (buffer);
89 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
94 #ifdef DCPOMATIC_WINDOWS
96 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
102 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
106 if (type != REG_SZ) {
110 wstring value (data / sizeof (wchar_t), L'\0');
111 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
116 info = string (value.begin(), value.end());
126 /** @return Path of the Contents directory in the .app */
127 boost::filesystem::path
130 uint32_t size = 1024;
132 if (_NSGetExecutablePath (buffer, &size)) {
133 throw StringError ("_NSGetExecutablePath failed");
136 boost::filesystem::path path (buffer);
137 path = boost::filesystem::canonical (path);
138 path = path.parent_path ();
139 path = path.parent_path ();
145 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
147 #ifdef DCPOMATIC_WINDOWS
148 SECURITY_ATTRIBUTES security;
149 security.nLength = sizeof (security);
150 security.bInheritHandle = TRUE;
151 security.lpSecurityDescriptor = 0;
153 HANDLE child_stderr_read;
154 HANDLE child_stderr_write;
155 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
156 log->log ("ffprobe call failed (could not CreatePipe)");
161 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
162 PathRemoveFileSpec (dir);
163 SetCurrentDirectory (dir);
165 STARTUPINFO startup_info;
166 ZeroMemory (&startup_info, sizeof (startup_info));
167 startup_info.cb = sizeof (startup_info);
168 startup_info.hStdError = child_stderr_write;
169 startup_info.dwFlags |= STARTF_USESTDHANDLES;
171 wchar_t command[512];
172 wcscpy (command, L"ffprobe.exe \"");
175 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
176 wcscat (command, file);
178 wcscat (command, L"\"");
180 PROCESS_INFORMATION process_info;
181 ZeroMemory (&process_info, sizeof (process_info));
182 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
183 log->log ("ffprobe call failed (could not CreateProcess)");
187 FILE* o = fopen (out.string().c_str(), "w");
189 log->log ("ffprobe call failed (could not create output file)");
193 CloseHandle (child_stderr_write);
198 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
201 fwrite (buffer, read, 1, o);
206 WaitForSingleObject (process_info.hProcess, INFINITE);
207 CloseHandle (process_info.hProcess);
208 CloseHandle (process_info.hThread);
209 CloseHandle (child_stderr_read);
212 #ifdef DCPOMATIC_LINUX
213 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
214 log->log (String::compose ("Probing with %1", ffprobe));
215 system (ffprobe.c_str ());
219 boost::filesystem::path path = app_contents();
223 string ffprobe = path.string() + " \"" + content.string() + "\" 2> \"" + out.string() + "\"";
224 log->log (String::compose ("Probing with %1", ffprobe));
225 system (ffprobe.c_str ());
229 list<pair<string, string> >
232 list<pair<string, string> > m;
234 #ifdef DCPOMATIC_LINUX
235 FILE* f = setmntent ("/etc/mtab", "r");
241 struct mntent* mnt = getmntent (f);
246 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
255 boost::filesystem::path
258 #ifdef DCPOMATIC_WINDOWS
260 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
261 PathRemoveFileSpec (dir);
263 boost::filesystem::path path = dir;
264 path /= "openssl.exe";
267 /* We assume that it's on the path for Linux and OS X */