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"
25 #include "exceptions.h"
27 #include <libavformat/avio.h>
29 #include <boost/algorithm/string.hpp>
30 #ifdef DCPOMATIC_LINUX
34 #ifdef DCPOMATIC_WINDOWS
41 #include <sys/sysctl.h>
42 #include <mach-o/dyld.h>
43 #include <IOKit/pwr_mgt/IOPMLib.h>
45 #ifdef DCPOMATIC_POSIX
46 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
55 #define LOG_GENERAL(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
56 #define LOG_ERROR(...) log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_ERROR);
57 #define LOG_ERROR_NC(...) log->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
65 using std::runtime_error;
66 using boost::shared_ptr;
68 /** @param s Number of seconds to sleep for */
70 dcpomatic_sleep (int s)
72 #ifdef DCPOMATIC_POSIX
75 #ifdef DCPOMATIC_WINDOWS
80 /** @return A string of CPU information (model name etc.) */
86 #ifdef DCPOMATIC_LINUX
87 /* This use of ifstream is ok; the filename can never
90 ifstream f ("/proc/cpuinfo");
94 if (boost::algorithm::starts_with (l, "model name")) {
95 string::size_type const c = l.find (':');
96 if (c != string::npos) {
97 info = l.substr (c + 2);
105 size_t N = sizeof (buffer);
106 if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
111 #ifdef DCPOMATIC_WINDOWS
113 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key) != ERROR_SUCCESS) {
119 if (RegQueryValueEx (key, L"ProcessorNameString", 0, &type, 0, &data) != ERROR_SUCCESS) {
123 if (type != REG_SZ) {
127 wstring value (data / sizeof (wchar_t), L'\0');
128 if (RegQueryValueEx (key, L"ProcessorNameString", 0, 0, reinterpret_cast<LPBYTE> (&value[0]), &data) != ERROR_SUCCESS) {
133 info = string (value.begin(), value.end());
143 /** @return Path of the Contents directory in the .app */
144 boost::filesystem::path
147 uint32_t size = 1024;
149 if (_NSGetExecutablePath (buffer, &size)) {
150 throw runtime_error ("_NSGetExecutablePath failed");
153 boost::filesystem::path path (buffer);
154 path = boost::filesystem::canonical (path);
155 path = path.parent_path ();
156 path = path.parent_path ();
161 boost::filesystem::path
164 #ifdef DCPOMATIC_LINUX
165 char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
169 return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
171 #ifdef DCPOMATIC_WINDOWS
173 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
174 PathRemoveFileSpec (dir);
175 boost::filesystem::path path = dir;
176 return path.parent_path();
179 return app_contents() / "Resources";
184 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
186 #ifdef DCPOMATIC_WINDOWS
187 SECURITY_ATTRIBUTES security;
188 security.nLength = sizeof (security);
189 security.bInheritHandle = TRUE;
190 security.lpSecurityDescriptor = 0;
192 HANDLE child_stderr_read;
193 HANDLE child_stderr_write;
194 if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
195 LOG_ERROR_NC ("ffprobe call failed (could not CreatePipe)");
200 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
201 PathRemoveFileSpec (dir);
202 SetCurrentDirectory (dir);
204 STARTUPINFO startup_info;
205 ZeroMemory (&startup_info, sizeof (startup_info));
206 startup_info.cb = sizeof (startup_info);
207 startup_info.hStdError = child_stderr_write;
208 startup_info.dwFlags |= STARTF_USESTDHANDLES;
210 wchar_t command[512];
211 wcscpy (command, L"ffprobe.exe \"");
214 MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
215 wcscat (command, file);
217 wcscat (command, L"\"");
219 PROCESS_INFORMATION process_info;
220 ZeroMemory (&process_info, sizeof (process_info));
221 if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
222 LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
226 FILE* o = fopen_boost (out, "w");
228 LOG_ERROR_NC (N_("ffprobe call failed (could not create output file)"));
232 CloseHandle (child_stderr_write);
237 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
240 fwrite (buffer, read, 1, o);
245 WaitForSingleObject (process_info.hProcess, INFINITE);
246 CloseHandle (process_info.hProcess);
247 CloseHandle (process_info.hThread);
248 CloseHandle (child_stderr_read);
251 #ifdef DCPOMATIC_LINUX
252 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
253 LOG_GENERAL (N_("Probing with %1"), ffprobe);
254 system (ffprobe.c_str ());
258 boost::filesystem::path path = app_contents();
262 string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\"";
263 LOG_GENERAL (N_("Probing with %1"), ffprobe);
264 system (ffprobe.c_str ());
268 list<pair<string, string> >
271 list<pair<string, string> > m;
273 #ifdef DCPOMATIC_LINUX
274 FILE* f = setmntent ("/etc/mtab", "r");
280 struct mntent* mnt = getmntent (f);
285 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
294 boost::filesystem::path
297 #ifdef DCPOMATIC_WINDOWS
299 GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
300 PathRemoveFileSpec (dir);
302 boost::filesystem::path path = dir;
303 path /= "openssl.exe";
306 /* We assume that it's on the path for Linux and OS X */
312 /* Apparently there is no way to create an ofstream using a UTF-8
313 filename under Windows. We are hence reduced to using fopen
317 fopen_boost (boost::filesystem::path p, string t)
319 #ifdef DCPOMATIC_WINDOWS
320 wstring w (t.begin(), t.end());
321 /* c_str() here should give a UTF-16 string */
322 return _wfopen (p.c_str(), w.c_str ());
324 return fopen (p.c_str(), t.c_str ());
329 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
331 #ifdef DCPOMATIC_WINDOWS
332 return _fseeki64 (stream, offset, whence);
334 return fseek (stream, offset, whence);
341 #ifdef DCPOMATIC_WINDOWS
342 SetThreadExecutionState (ES_SYSTEM_REQUIRED);
349 /* We should use this */
350 // IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
351 /* but it's not available on 10.5, so we use this */
352 IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, &_assertion_id);
359 IOPMAssertionRelease (_assertion_id);
364 start_batch_converter (boost::filesystem::path dcpomatic)
366 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_WINDOWS)
367 boost::filesystem::path batch = dcpomatic.parent_path() / "dcpomatic2_batch";
371 boost::filesystem::path batch = dcpomatic.parent_path ();
372 batch = batch.parent_path (); // MacOS
373 batch = batch.parent_path (); // Contents
374 batch = batch.parent_path (); // DCP-o-matic.app
375 batch = batch.parent_path (); // Applications
376 batch /= "DCP-o-matic\\ 2\\ Batch\\ Converter.app";
379 batch /= "dcpomatic2_batch";
382 #if defined(DCPOMATIC_LINUX) || defined(DCPOMATIC_OSX)
385 std::cout << "start " << batch << " from " << dcpomatic << "\n";
386 int const r = system (batch.string().c_str());
387 exit (WEXITSTATUS (r));
391 #ifdef DCPOMATIC_WINDOWS
392 STARTUPINFO startup_info;
393 ZeroMemory (&startup_info, sizeof (startup_info));
394 startup_info.cb = sizeof (startup_info);
396 PROCESS_INFORMATION process_info;
397 ZeroMemory (&process_info, sizeof (process_info));
400 MultiByteToWideChar (CP_UTF8, 0, batch.string().c_str(), -1, cmd, sizeof(cmd));
401 CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info);
408 #ifdef DCPOMATIC_WINDOWS
409 return (uint64_t) GetCurrentThreadId ();
411 return (uint64_t) pthread_self ();
416 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
418 #ifdef DCPOMATIC_WINDOWS
419 int const length = (file.string().length() + 1) * 2;
420 char* utf8 = new char[length];
421 WideCharToMultiByte (CP_UTF8, 0, file.c_str(), -1, utf8, length, 0, 0);
422 int const r = avio_open (s, utf8, flags);
426 return avio_open (s, file.c_str(), flags);
430 #ifdef DCPOMATIC_WINDOWS
432 maybe_open_console ()
434 if (Config::instance()->win32_console ()) {
437 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
438 int hCrt = _open_osfhandle((intptr_t) handle_out, _O_TEXT);
439 FILE* hf_out = _fdopen(hCrt, "w");
440 setvbuf(hf_out, NULL, _IONBF, 1);
443 HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
444 hCrt = _open_osfhandle((intptr_t) handle_in, _O_TEXT);
445 FILE* hf_in = _fdopen(hCrt, "r");
446 setvbuf(hf_in, NULL, _IONBF, 128);