Skip to content

Commit

Permalink
fix: 调整minicap的执行逻辑
Browse files Browse the repository at this point in the history
将stderr直接重定向到stdout, 避免某些特定adb无法正确forward stderr的问题
修复minicap direct的处理错误
  • Loading branch information
neko-para committed Oct 16, 2023
1 parent 03418a0 commit 473a77d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions source/MaaControlUnit/Invoke/InvokeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ std::optional<std::string> InvokeApp::invoke_bin_stdout(const std::string& extra
return command(invoke_bin_argv_.gen(argv_replace_));
}

std::shared_ptr<IOHandler> InvokeApp::invoke_bin(const std::string& extra)
std::shared_ptr<IOHandler> InvokeApp::invoke_bin(const std::string& extra, bool wants_stderr)
{
LogFunc;

Expand All @@ -118,7 +118,7 @@ std::shared_ptr<IOHandler> InvokeApp::invoke_bin(const std::string& extra)

merge_replacement({ { "{BIN_WORKING_FILE}", tempname_ }, { "{BIN_EXTRA_PARAMS}", extra } });
LogInfo << invoke_bin_argv_.gen(argv_replace_);
auto cmd_ret = io_ptr_->interactive_shell(invoke_bin_argv_.gen(argv_replace_), true);
auto cmd_ret = io_ptr_->interactive_shell(invoke_bin_argv_.gen(argv_replace_), wants_stderr);

return cmd_ret;
}
Expand Down
2 changes: 1 addition & 1 deletion source/MaaControlUnit/Invoke/InvokeApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InvokeApp : public UnitBase
bool chmod();

std::optional<std::string> invoke_bin_stdout(const std::string& extra);
std::shared_ptr<IOHandler> invoke_bin(const std::string& extra);
std::shared_ptr<IOHandler> invoke_bin(const std::string& extra, bool wants_stderr = false);
std::shared_ptr<IOHandler> invoke_app(const std::string& package);

#ifdef MAA_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion source/MaaControlUnit/Screencap/Minicap/MinicapDirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ std::optional<cv::Mat> MinicapDirect::screencap()
}

return screencap_helper_.process_data(
res.value(), std::bind(&ScreencapHelper::decode_jpg, &screencap_helper_, std::placeholders::_1));
res.value(), std::bind(&ScreencapHelper::trunc_decode_jpg, &screencap_helper_, std::placeholders::_1));
}
MAA_CTRL_UNIT_NS_END
12 changes: 8 additions & 4 deletions source/MaaControlUnit/Screencap/Minicap/MinicapStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,31 @@ bool MinicapStream::init(int swidth, int sheight)
}

std::string outputBuffer;
while (true) {
int i = 50;
while (--i) {
auto res = process_handle_->read(5);
if (res.length() > 0) {
LogInfo << "minicap stderr:" << res;
LogInfo << "minicap stdout:" << res;
outputBuffer.append(res);
}
if (outputBuffer.find("Allocating") != std::string::npos) {
break;
}
}

if (i == 0) {
return false;
}

auto serial_host = argv_replace_["{ADB_SERIAL}"];
auto shp = serial_host.find(':');
std::string local = "127.0.0.1";
if (shp != std::string::npos) {
local = serial_host.substr(0, shp);
}

LogInfo << "minicap try listen at:" << local;
LogInfo << "minicap try connect to:" << local;

// stream_handle_ = io_ptr_->tcp("172.27.176.1", 1313);
stream_handle_ = io_ptr_->tcp(local, 1313);

if (!stream_handle_) {
Expand Down
10 changes: 10 additions & 0 deletions source/MaaControlUnit/Screencap/ScreencapHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ std::optional<cv::Mat> ScreencapHelper::decode_png(const std::string& buffer)
return decode(buffer);
}

std::optional<cv::Mat> ScreencapHelper::trunc_decode_jpg(const std::string& buffer)
{
auto pos = buffer.find("\xFF\xD8\xFF");
auto truncbuf = buffer.substr(pos);
if (!check_head_tail(truncbuf, "\xFF\xD8\xFF", "\xFF\xD9")) {
return std::nullopt;
}
return decode(truncbuf);
}

std::optional<cv::Mat> ScreencapHelper::decode_jpg(const std::string& buffer)
{
if (!check_head_tail(buffer, "\xFF\xD8\xFF", "\xFF\xD9")) {
Expand Down
1 change: 1 addition & 0 deletions source/MaaControlUnit/Screencap/ScreencapHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ScreencapHelper
std::optional<cv::Mat> decode_raw(const std::string& buffer);
std::optional<cv::Mat> decode_gzip(const std::string& buffer);
std::optional<cv::Mat> decode_png(const std::string& buffer);
std::optional<cv::Mat> trunc_decode_jpg(const std::string& buffer);
std::optional<cv::Mat> decode_jpg(const std::string& buffer);
std::optional<cv::Mat> decode(const std::string& buffer);
static bool clean_cr(std::string& buffer);
Expand Down
2 changes: 1 addition & 1 deletion source/MaaToolKit/Device/AdbConfigDef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ inline static const json::value kAdbConfig = R"(
"-s",
"{ADB_SERIAL}",
"shell",
"export LD_LIBRARY_PATH=/data/local/tmp/; \"/data/local/tmp/{BIN_WORKING_FILE}\" {BIN_EXTRA_PARAMS}"
"export LD_LIBRARY_PATH=/data/local/tmp/; \"/data/local/tmp/{BIN_WORKING_FILE}\" {BIN_EXTRA_PARAMS} 2>&1"
],
"InvokeApp": [
"{ADB}",
Expand Down

0 comments on commit 473a77d

Please sign in to comment.