Skip to content

Commit d6d188b

Browse files
committed
Various improvements in plugin code.
1 parent c443443 commit d6d188b

5 files changed

Lines changed: 119 additions & 37 deletions

File tree

plugins/code/common/PipingManager/PipingManager.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
#include <PipingManager.h>
4242

4343
#ifdef WIN32
44+
#ifndef NOMINMAX
4445
#define NOMINMAX
46+
#endif
4547
#include <Windows.h>
4648
#else
4749
#include <signal.h>
@@ -81,7 +83,12 @@ class CircularFifo
8183
void SetSize(size_t size)
8284
{
8385
mSize = size;
84-
mBuffer.resize(size);
86+
try {
87+
mBuffer.reserve(size);
88+
}
89+
catch (std::exception&) {
90+
throw std::runtime_error("Failed to allocate pipe buffer.");
91+
}
8592
mFree = size;
8693
}
8794
int Append(char* buffer, size_t size)
@@ -428,7 +435,12 @@ PipeData::PipeData(std::string name, size_t maxbuf, pipe_type_t type, int id, bo
428435
mClient = client;
429436
mTimeout = false;
430437

431-
mTempBuf.resize(mBufferSize);
438+
try {
439+
mTempBuf.reserve(mBufferSize);
440+
}
441+
catch (std::exception&) {
442+
throw std::runtime_error("Failed to allocate pipe buffer.");
443+
}
432444
if (type == INPUT_PIPE || type == DUPLEX_PIPE) mInBuf.SetSize(mBufferSize);
433445
if (type == OUTPUT_PIPE || type == DUPLEX_PIPE) mOutBuf.SetSize(mBufferSize);
434446

plugins/code/common/SystemCalls/SystemCalls.cpp

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,57 @@ std::vector<std::string> split(const std::string& s, const std::string& delim) {
9898
return elems;
9999
}
100100

101+
static
102+
std::string
103+
replaceSubstring(const std::string& in, const std::string& substring, const std::string& replaceWith) {
104+
std::string output = in;
105+
size_t index = 0;
106+
107+
if (substring.empty())
108+
return output;
109+
110+
for (;;) {
111+
index = output.find(substring, index);
112+
if (index == std::string::npos) break;
113+
output.replace(index, substring.size(), replaceWith);
114+
}
115+
116+
return output;
117+
}
118+
119+
static
120+
std::string
121+
replaceSubstringWithinDelimiters(const std::string& str, const std::string& sub, const std::string& rep, const std::string& delim){
122+
std::string output = str;
123+
if (delim.empty() || sub.empty())
124+
return output;
125+
126+
size_t startDelim = output.find(delim, 0);
127+
while(startDelim != std::string::npos){
128+
size_t endDelim = output.find(delim, startDelim+1);
129+
if(endDelim == std::string::npos)
130+
break;
131+
132+
size_t posSub = output.find(sub, startDelim);
133+
while(posSub != std::string::npos && posSub < endDelim){
134+
output.replace(output.begin() + posSub, output.begin() + posSub + sub.size(), rep);
135+
136+
endDelim = output.find(delim, startDelim+1);
137+
posSub = output.find(sub, startDelim);
138+
}
139+
140+
startDelim = output.find(delim, endDelim+1);
141+
}
142+
143+
return output;
144+
}
101145

102146
static
103147
inline
104148
std::vector<std::string> splitQuotedString(const std::string &in, std::string delimiter) {
149+
auto inproxy = replaceSubstringWithinDelimiters(in, " ", "$%SPA&^CE!%", "\"");
105150
std::vector<std::string> items;
106-
std::vector<std::string> items_delim = split(in, delimiter);
151+
std::vector<std::string> items_delim = split(inproxy, delimiter);
107152

108153
std::string cur_item;
109154
for (auto i : items_delim)
@@ -142,6 +187,10 @@ std::vector<std::string> splitQuotedString(const std::string &in, std::string de
142187
cur_item.clear();
143188
}
144189

190+
for (auto& x : items) {
191+
x = replaceSubstring(x, "$%SPA&^CE!%", " ");
192+
}
193+
145194
return items;
146195
}
147196

@@ -159,11 +208,11 @@ std::string& trim(std::string& s, const char* t = "\"\n\t ")
159208
static
160209
bool isStringMultibyte(const std::string& str)
161210
{
162-
for (unsigned int i = 0; i < str.size(); i++) {
163-
if ((0x80 & str[i]) != 0)
164-
return true;
165-
}
166-
return false;
211+
for (unsigned int i = 0; i < str.size(); i++) {
212+
if ((0x80 & str[i]) != 0)
213+
return true;
214+
}
215+
return false;
167216
}
168217
#endif
169218

@@ -187,7 +236,23 @@ system_call_status_t startProcess(std::string command, std::string logfile, Proc
187236
sa.lpSecurityDescriptor = NULL;
188237
sa.bInheritHandle = TRUE;
189238

190-
log_handle = CreateFile(logfile.c_str(),
239+
LPWSTR logfile_wide = NULL;
240+
if (isStringMultibyte(logfile) == false) {
241+
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
242+
std::wstring wide_str = myconv.from_bytes(logfile);
243+
wide_str.push_back(NULL); // need to manually null-terminate the string
244+
logfile_wide = new WCHAR[wide_str.length()];
245+
memcpy(logfile_wide, wide_str.c_str(), wide_str.length() * sizeof(wchar_t));
246+
}
247+
else {
248+
int wideLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, logfile.c_str(), (int)logfile.size() + 1, logfile_wide, 0);
249+
if (wideLength != 0) {
250+
logfile_wide = new WCHAR[wideLength];
251+
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, logfile.c_str(), (int)logfile.size() + 1, logfile_wide, wideLength);
252+
}
253+
}
254+
255+
log_handle = CreateFileW(logfile_wide,
191256
FILE_APPEND_DATA,
192257
FILE_SHARE_WRITE | FILE_SHARE_READ,
193258
&sa,
@@ -199,34 +264,35 @@ system_call_status_t startProcess(std::string command, std::string logfile, Proc
199264
processData.startupInfo.hStdInput = NULL;
200265
processData.startupInfo.hStdOutput = log_handle;
201266
processData.startupInfo.hStdError = log_handle;
267+
268+
if (logfile_wide != NULL)
269+
delete [] logfile_wide;
270+
}
271+
272+
LPWSTR command_line_wide = NULL;
273+
if (isStringMultibyte(command_line) == false) {
274+
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
275+
std::wstring wide_str = myconv.from_bytes(command_line);
276+
wide_str.push_back(NULL); // need to manually null-terminate the string
277+
command_line_wide = new WCHAR[wide_str.length()];
278+
memcpy(command_line_wide, wide_str.c_str(), wide_str.length() * sizeof(wchar_t));
279+
}
280+
else {
281+
int wideLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, command_line.c_str(), (int)command_line.size() + 1, command_line_wide, 0);
282+
if (wideLength != 0) {
283+
command_line_wide = new WCHAR[wideLength];
284+
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, command_line.c_str(), (int)command_line.size() + 1, command_line_wide, wideLength);
285+
}
202286
}
203287

204-
LPWSTR command_line_wide = NULL;
205-
int wideLength;
206-
207-
if (isStringMultibyte(command_line) == false) {
208-
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
209-
std::wstring wide_str = myconv.from_bytes(command_line);
210-
wide_str.push_back(NULL); // need to manually null-terminate the string
211-
command_line_wide = new WCHAR[wide_str.length()];
212-
memcpy(command_line_wide, wide_str.c_str(), wide_str.length() * sizeof(wchar_t));
213-
}
214-
else {
215-
wideLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, command_line.c_str(), (int)command_line.size() + 1, command_line_wide, 0);
216-
if (wideLength != 0) {
217-
command_line_wide = new WCHAR[wideLength];
218-
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, command_line.c_str(), (int)command_line.size() + 1, command_line_wide, wideLength);
219-
}
220-
}
221-
222288
BOOL status = CreateProcessW(NULL, command_line_wide, NULL, NULL, TRUE, 0, NULL, NULL, &processData.startupInfo, &processData.processInfo);
223289
// closing unneeded handles
224290
CloseHandle(processData.processInfo.hThread);
225291
CloseHandle(processData.startupInfo.hStdInput);
226292
CloseHandle(log_handle);
227-
if (command_line_wide != NULL)
228-
delete[] command_line_wide;
229-
293+
if (command_line_wide != NULL)
294+
delete[] command_line_wide;
295+
230296
if (status == 0) {
231297
return SYSCALL_STATUS_CALL_ERROR;
232298
}

plugins/code/hevc_dec/beamr/src/hevc_dec_beamr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static HevcDecStatus init(HevcDecHandle handle, const HevcDecInitParams* init_pa
145145
state->ctrl->debug_level = string2int(name, value, 0, 2);
146146
}
147147
else {
148-
state->ctrl->msg += "\nUnknown XML property: " + name;
148+
state->ctrl->msg += "\nUnknown property: " + name;
149149
}
150150
}
151151
catch (std::exception& e) {

plugins/code/hevc_enc/beamr/src/hevc_enc_beamr.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static const PropertyInfo propertyInfo[] = {
9090
"false", NULL, 0, 1, ACCESS_TYPE_WRITE_INIT},
9191
{"uhd_bd", PROPERTY_TYPE_BOOLEAN, "Indicates UHD-BD encoding.", "false", NULL, 0, 1, ACCESS_TYPE_WRITE_INIT},
9292

93-
// Properties visible in XML interface
93+
// Properties visible in the interface
9494
{"preset", PROPERTY_TYPE_STRING, "Encoder preset (initial state of encoder configuration).", "medium",
9595
"insanely_slow:ultra_slow:very_slow:slower:slow:medium:medium_plus:fast:faster:ultra_fast:insanely_fast", 0, 1,
9696
ACCESS_TYPE_USER},
@@ -307,17 +307,21 @@ static Status init(HevcEncHandle handle, const HevcEncInitParams* init_params) {
307307
else if ("debug_level" == name) {
308308
state->ctrl->debug_level = (int)parseInt(name, value, schema, count);
309309
}
310+
else if ("color_space" == name) {
311+
continue; //hardcoded to i420 anyways
312+
}
310313
else {
311-
state->ctrl->msg += "\nUnknown XML property: " + name;
314+
state->ctrl->msg += "\nUnknown property: " + name;
312315
}
313316
}
314317
catch (std::exception& e) {
315318
state->ctrl->msg += "\n" + std::string(e.what());
316319
}
317320
}
318321

319-
if (state->ctrl->msg.size())
322+
if (state->ctrl->msg.size()) {
320323
return STATUS_ERROR;
324+
}
321325

322326
state->encoder->init(*state->ctrl);
323327

plugins/code/hevc_enc/beamr/src/hevc_enc_beamr_utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int64_t parseInt(const std::string& name, const std::string& value, const Proper
6666
return string2int(name, value, minVal, maxVal);
6767
}
6868
}
69-
throw std::runtime_error("Unknown XML property: " + name);
69+
throw std::runtime_error("Unknown property: " + name);
7070
}
7171

7272
std::string parseString(const std::string& name, const std::string& value, const PropertyInfo* schema, size_t count) {
@@ -82,7 +82,7 @@ std::string parseString(const std::string& name, const std::string& value, const
8282
return value;
8383
}
8484
}
85-
throw std::runtime_error("Unknown XML property: " + name);
85+
throw std::runtime_error("Unknown property: " + name);
8686
}
8787

8888
std::string parseStringList(const std::string& name,
@@ -106,7 +106,7 @@ bool parseBool(const std::string& name, const std::string& value, const Property
106106
return string2bool(name, value);
107107
}
108108
}
109-
throw std::runtime_error("Unknown XML property: " + name);
109+
throw std::runtime_error("Unknown property: " + name);
110110
}
111111

112112
FramePeriod string2FramePeriod(const std::string& s) {

0 commit comments

Comments
 (0)