Fix quoting for ffprobe.
[dcpomatic.git] / src / lib / cross.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <fstream>
21 #include <boost/algorithm/string.hpp>
22 #include "cross.h"
23 #include "compose.hpp"
24 #include "log.h"
25 #ifdef DVDOMATIC_POSIX
26 #include <unistd.h>
27 #endif
28 #ifdef DVDOMATIC_WINDOWS
29 #include <windows.h>
30 #undef DATADIR
31 #include <shlwapi.h>
32 #endif
33 #ifdef DVDOMATIC_OSX
34 #include <sys/sysctl.h>
35 #endif
36
37 using std::pair;
38 using std::ifstream;
39 using std::string;
40 using boost::shared_ptr;
41
42 void
43 dvdomatic_sleep (int s)
44 {
45 #ifdef DVDOMATIC_POSIX
46         sleep (s);
47 #endif
48 #ifdef DVDOMATIC_WINDOWS
49         Sleep (s * 1000);
50 #endif
51 }
52
53 /** @return A pair containing CPU model name and the number of processors */
54 pair<string, int>
55 cpu_info ()
56 {
57         pair<string, int> info;
58         info.second = 0;
59         
60 #ifdef DVDOMATIC_LINUX
61         ifstream f ("/proc/cpuinfo");
62         while (f.good ()) {
63                 string l;
64                 getline (f, l);
65                 if (boost::algorithm::starts_with (l, "model name")) {
66                         string::size_type const c = l.find (':');
67                         if (c != string::npos) {
68                                 info.first = l.substr (c + 2);
69                         }
70                 } else if (boost::algorithm::starts_with (l, "processor")) {
71                         ++info.second;
72                 }
73         }
74 #endif
75
76 #ifdef DVDOMATIC_OSX
77         size_t N = sizeof (info.second);
78         sysctlbyname ("hw.ncpu", &info.second, &N, 0, 0);
79         char buffer[64];
80         N = sizeof (buffer);
81         if (sysctlbyname ("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
82                 info.first = buffer;
83         }
84 #endif          
85
86         return info;
87 }
88
89 void
90 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out, shared_ptr<Log> log)
91 {
92 #ifdef DVDOMATIC_WINDOWS
93         SECURITY_ATTRIBUTES security;
94         security.nLength = sizeof (security);
95         security.bInheritHandle = TRUE;
96         security.lpSecurityDescriptor = 0;
97
98         HANDLE child_stderr_read;
99         HANDLE child_stderr_write;
100         if (!CreatePipe (&child_stderr_read, &child_stderr_write, &security, 0)) {
101                 log->log ("ffprobe call failed (could not CreatePipe)");
102                 return;
103         }
104
105         wchar_t dir[512];
106         GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
107         PathRemoveFileSpec (dir);
108         SetCurrentDirectory (dir);
109
110         STARTUPINFO startup_info;
111         ZeroMemory (&startup_info, sizeof (startup_info));
112         startup_info.cb = sizeof (startup_info);
113         startup_info.hStdError = child_stderr_write;
114         startup_info.dwFlags |= STARTF_USESTDHANDLES;
115
116         wchar_t command[512];
117         wcscpy (command, L"ffprobe.exe ");
118
119         wchar_t file[512];
120         MultiByteToWideChar (CP_UTF8, 0, content.string().c_str(), -1, file, sizeof(file));
121         wcscat (command, file);
122
123         PROCESS_INFORMATION process_info;
124         ZeroMemory (&process_info, sizeof (process_info));
125         if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
126                 log->log ("ffprobe call failed (could not CreateProcess)");
127                 return;
128         }
129
130         FILE* o = fopen (out.string().c_str(), "w");
131         if (!o) {
132                 log->log ("ffprobe call failed (could not create output file)");
133                 return;
134         }
135
136         CloseHandle (child_stderr_write);
137
138         while (1) {
139                 char buffer[512];
140                 DWORD read;
141                 if (!ReadFile(child_stderr_read, buffer, sizeof(buffer), &read, 0) || read == 0) {
142                         break;
143                 }
144                 fwrite (buffer, read, 1, o);
145         }
146
147         fclose (o);
148
149         WaitForSingleObject (process_info.hProcess, INFINITE);
150         CloseHandle (process_info.hProcess);
151         CloseHandle (process_info.hThread);
152         CloseHandle (child_stderr_read);
153 #else
154         string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
155         log->log (String::compose ("Probing with %1", ffprobe));
156         system (ffprobe.c_str ());
157 #endif  
158 }