-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeEncoder.cpp
More file actions
233 lines (206 loc) · 5.44 KB
/
PipeEncoder.cpp
File metadata and controls
233 lines (206 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "PipeEncoder.h"
#include "Formats.h"
#include <cstring>
#include <string>
PipeEncoder::PipeEncoder(const wchar_t *command_line)
: child_stdin_rd(NULL), child_stdin_wr(NULL),
child_stdout_rd(NULL), child_stdout_wr(NULL)
{
memset(&proc_info, 0, sizeof(proc_info));
command = (wchar_t*)malloc((wcslen(command_line) + 1)*sizeof(wchar_t));
if (command)
wcscpy(command, command_line);
}
enum
{
ERROR_OUT_OF_MEMORY = ENCODER_ERROR_MASK,
ERROR_CREATE_STDIN_PIPE,
ERROR_CREATE_STDOUT_PIPE,
ERROR_SET_INFORMATION_STDIN,
ERROR_SET_INFORMATION_STDOUT,
ERROR_CREATE_PROCESS,
ERROR_WRITE_INTO_PIPE,
ERROR_WRITE_NOT_FULL,
ERROR_EXIT_PROCESS,
};
wchar_t* PipeEncoder::GetErrorString(DWORD error)
{
switch(error)
{
case ERROR_OUT_OF_MEMORY:
return L"Out of memory";
case ERROR_CREATE_STDIN_PIPE:
return L"Can't create stdin pipe for child process";
case ERROR_CREATE_STDOUT_PIPE:
return L"Can't create stdout pipe for child process";
case ERROR_SET_INFORMATION_STDIN:
return L"Can't set information for stdin pipe";
case ERROR_SET_INFORMATION_STDOUT:
return L"Can't set information for stdout pipe";
case ERROR_CREATE_PROCESS:
return L"Can't start child process";
case ERROR_WRITE_INTO_PIPE:
return L"Write attempt into pipe was failed";
case ERROR_WRITE_NOT_FULL:
return L"Write into pipe was not complete (partially)";
case ERROR_EXIT_PROCESS:
return L"Process exited before end of recording";
default:
return NULL;
}
}
static std::wstring AutoReplace(const wchar_t *command, const WAVEFORMATEX *format)
{
std::wstring res;
wchar_t buff[50];
for (int i=0; command[i];)
{
if (command[i] == '%')
{
if (!wcsncmp(L"%b%", command+i, 3))
{
swprintf(buff, L"%d", format->wBitsPerSample);
res += buff;
i+=3;
continue;
}
else if (!wcsncmp(L"%c%", command+i, 3))
{
swprintf(buff, L"%d", format->nChannels);
res += buff;
i += 3;
continue;
}
else if (!wcsncmp(L"%r%", command+i, 3))
{
swprintf(buff, L"%d", format->nSamplesPerSec);
res += buff;
i += 3;
continue;
}
else if (!wcsncmp(L"%f%", command+i, 3))
{
WORD id;
if (!FormatDetect(format, &id))
id = -1;
switch(id)
{
case WAVE_FORMAT_PCM:
swprintf(buff, L"%c%d%s",
format->wBitsPerSample==8?L'u':L's',
format->wBitsPerSample,
format->wBitsPerSample<=8?L"":L"le");
res += buff;
break;
case WAVE_FORMAT_IEEE_FLOAT:
if (format->wBitsPerSample == 32)
res += L"f32le";
else if (format->wBitsPerSample == 64)
res += L"f64le";
else goto unk;
break;
default:
unk:
res += L"unknown";
}
i += 3;
continue;
}
}
res += command[i];
++i;
}
return res;
}
DWORD PipeEncoder::Init(const WAVEFORMATEX *format)
{
SECURITY_ATTRIBUTES attr;
HANDLE tmp = NULL;
DWORD res = 0;
STARTUPINFO start_info;
WCHAR* tmp_command = NULL;
std::wstring new_command;
if (!command)
return ERROR_OUT_OF_MEMORY;
attr.nLength = sizeof(SECURITY_ATTRIBUTES);
attr.bInheritHandle = TRUE;
attr.lpSecurityDescriptor = NULL;
#define ERROR_EXIT(error) if (1) {res = error; goto Exit;} else (void*)0
if (!CreatePipe(&child_stdin_rd, &child_stdin_wr, &attr, 1<<20))
ERROR_EXIT(ERROR_CREATE_STDIN_PIPE);
if (!SetHandleInformation(child_stdin_wr, HANDLE_FLAG_INHERIT, 0))
ERROR_EXIT(ERROR_SET_INFORMATION_STDIN);
if (!CreatePipe(&child_stdout_rd, &child_stdout_wr, &attr, 1<<20))
ERROR_EXIT(ERROR_CREATE_STDOUT_PIPE);
if (!SetHandleInformation(child_stdout_rd, HANDLE_FLAG_INHERIT, 0))
ERROR_EXIT(ERROR_SET_INFORMATION_STDOUT);
memset(&start_info, 0, sizeof(STARTUPINFO));
start_info.cb = sizeof(STARTUPINFO);
start_info.hStdError = child_stdout_wr;
start_info.hStdOutput = child_stdout_wr;
start_info.hStdInput = child_stdin_rd;
start_info.dwFlags |= STARTF_USESTDHANDLES;
new_command = AutoReplace(command, format);
tmp_command = (WCHAR*)malloc((new_command.length()+1)*sizeof(WCHAR));
if (!tmp_command)
return ERROR_OUT_OF_MEMORY;
wcscpy(tmp_command, new_command.c_str());
if (!CreateProcess(NULL, tmp_command, NULL, NULL, TRUE, 0, NULL, NULL, &start_info, &proc_info))
ERROR_EXIT(ERROR_CREATE_PROCESS);
free(tmp_command);
return 0;
#undef ERROR_EXIT
Exit:
if (tmp_command)
free(tmp_command);
if (child_stdin_rd)
CloseHandle(child_stdin_rd);
if (child_stdin_wr)
CloseHandle(child_stdin_wr);
if (child_stdout_rd)
CloseHandle(child_stdout_rd);
if (child_stdout_wr)
CloseHandle(child_stdout_wr);
if (proc_info.hProcess)
CloseHandle(proc_info.hProcess);
if (proc_info.hThread)
CloseHandle(proc_info.hThread);
child_stdin_rd = NULL;
child_stdin_wr = NULL;
child_stdout_rd = NULL;
child_stdout_wr = NULL;
proc_info.hProcess = NULL;
proc_info.hThread = NULL;
return res;
}
DWORD PipeEncoder::Encode(BYTE *data, int size)
{
DWORD dwWritten;
DWORD exit;
GetExitCodeProcess(proc_info.hProcess,&exit);
if (exit != STILL_ACTIVE)
return ERROR_EXIT_PROCESS;
if (!WriteFile(child_stdin_wr, data, size, &dwWritten, NULL))
return ERROR_WRITE_INTO_PIPE;
if (dwWritten != size)
return ERROR_WRITE_NOT_FULL;
return 0;
}
DWORD PipeEncoder::Finalize()
{
if (child_stdin_rd)
CloseHandle(child_stdin_rd);
if (child_stdin_wr)
CloseHandle(child_stdin_wr);
if (child_stdout_rd)
CloseHandle(child_stdout_rd);
if (child_stdout_wr)
CloseHandle(child_stdout_wr);
if (proc_info.hProcess)
CloseHandle(proc_info.hProcess);
if (proc_info.hThread)
CloseHandle(proc_info.hThread);
if (command)
free(command);
return 0;
}