2026-10-28 08:48:12 -04:00
|
|
|
|
#include "moonlight_session.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <QDate>
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
#include <QFile>
|
2026-10-30 02:42:11 -04:00
|
|
|
|
#include <QProcessEnvironment>
|
2026-10-28 08:48:12 -04:00
|
|
|
|
#include <QTextStream>
|
2026-10-30 02:42:11 -04:00
|
|
|
|
#include <QTimer>
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
namespace vdi {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
namespace {
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
2026-10-30 02:42:11 -04:00
|
|
|
|
constexpr int kReadyStableMs = 750;
|
|
|
|
|
|
constexpr int kTerminateGraceMs = 2000;
|
|
|
|
|
|
|
|
|
|
|
|
bool isTestDecodeLine(const QByteArray& line) {
|
|
|
|
|
|
return line.toLower().contains("test decode successful");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isRendererLine(const QByteArray& line) {
|
|
|
|
|
|
const QByteArray lower = line.toLower();
|
|
|
|
|
|
return lower.contains("renderer '") && lower.contains(" chosen");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isTerminalErrorLine(const QByteArray& line) {
|
|
|
|
|
|
const QByteArray lower = line.toLower();
|
|
|
|
|
|
if (lower.contains("会话已终止")) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
const QByteArray marker("connection terminated:");
|
|
|
|
|
|
const int markerPos = lower.indexOf(marker);
|
|
|
|
|
|
if (markerPos < 0) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool ok = false;
|
|
|
|
|
|
const int code = lower.mid(markerPos + marker.size()).trimmed().toInt(&ok);
|
|
|
|
|
|
return !ok || code != 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightOutputParser::reset() {
|
|
|
|
|
|
buffer_.clear();
|
|
|
|
|
|
hasTestDecode_ = false;
|
|
|
|
|
|
hasRenderer_ = false;
|
|
|
|
|
|
hasTerminalError_ = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightOutputParser::append(const QByteArray& output) {
|
|
|
|
|
|
buffer_.append(output);
|
|
|
|
|
|
int newline = -1;
|
|
|
|
|
|
while ((newline = buffer_.indexOf('\n')) >= 0) {
|
|
|
|
|
|
const QByteArray line = buffer_.left(newline).trimmed();
|
|
|
|
|
|
buffer_.remove(0, newline + 1);
|
|
|
|
|
|
hasTestDecode_ = hasTestDecode_ || isTestDecodeLine(line);
|
|
|
|
|
|
hasRenderer_ = hasRenderer_ || isRendererLine(line);
|
|
|
|
|
|
hasTerminalError_ = hasTerminalError_ || isTerminalErrorLine(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MoonlightOutputParser::isReady() const {
|
|
|
|
|
|
return hasTestDecode_ && hasRenderer_ && !hasTerminalError_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MoonlightSession::MoonlightSession(QObject* parent) : QObject(parent) {
|
|
|
|
|
|
process_ = new QProcess(this);
|
|
|
|
|
|
process_->setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
|
|
connect(process_, &QProcess::started, this, &MoonlightSession::onProcessStarted);
|
|
|
|
|
|
connect(process_,
|
|
|
|
|
|
QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
|
|
|
|
|
|
&MoonlightSession::onProcessFinished);
|
|
|
|
|
|
connect(process_, &QProcess::errorOccurred, this, &MoonlightSession::onProcessError);
|
|
|
|
|
|
connect(process_, &QProcess::readyReadStandardOutput, this,
|
|
|
|
|
|
&MoonlightSession::onProcessOutput);
|
|
|
|
|
|
|
|
|
|
|
|
readyTimer_ = new QTimer(this);
|
|
|
|
|
|
readyTimer_->setSingleShot(true);
|
|
|
|
|
|
connect(readyTimer_, &QTimer::timeout, this, [this]() {
|
|
|
|
|
|
if (!readyEventDelivered_ && !terminalEventDelivered_ && isRunning()) {
|
|
|
|
|
|
readyEventDelivered_ = true;
|
|
|
|
|
|
emitEvent(EventType::kStreamReady, QStringLiteral("串流画面已就绪"));
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
killTimer_ = new QTimer(this);
|
|
|
|
|
|
killTimer_->setSingleShot(true);
|
|
|
|
|
|
connect(killTimer_, &QTimer::timeout, this, [this]() {
|
|
|
|
|
|
if (isRunning()) {
|
|
|
|
|
|
process_->kill();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
MoonlightSession::~MoonlightSession() {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
callback_ = nullptr;
|
|
|
|
|
|
readyTimer_->stop();
|
|
|
|
|
|
killTimer_->stop();
|
|
|
|
|
|
if (isRunning()) {
|
|
|
|
|
|
process_->kill();
|
|
|
|
|
|
process_->waitForFinished(1000);
|
|
|
|
|
|
}
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::setOptions(const Options& options) {
|
|
|
|
|
|
options_ = options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MoonlightSession::isRunning() const {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
return process_->state() != QProcess::NotRunning;
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-29 05:37:38 -04:00
|
|
|
|
qint64 MoonlightSession::processId() const {
|
|
|
|
|
|
return isRunning() ? process_->processId() : 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 02:42:11 -04:00
|
|
|
|
bool MoonlightSession::activateStreamWindow() {
|
|
|
|
|
|
if (!isRunning()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QProcess finder;
|
|
|
|
|
|
finder.start(QStringLiteral("xdotool"),
|
|
|
|
|
|
{QStringLiteral("search"), QStringLiteral("--onlyvisible"),
|
|
|
|
|
|
QStringLiteral("--pid"), QString::number(process_->processId()),
|
|
|
|
|
|
QStringLiteral("--class"), QStringLiteral("com.moonlight_stream.Moonlight")});
|
|
|
|
|
|
if (!finder.waitForFinished(500) || finder.exitCode() != 0) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const QString windowId = QString::fromUtf8(finder.readAllStandardOutput())
|
|
|
|
|
|
.split(QLatin1Char('\n'), Qt::SkipEmptyParts)
|
|
|
|
|
|
.value(0).trimmed();
|
|
|
|
|
|
if (windowId.isEmpty()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return QProcess::execute(QStringLiteral("xdotool"),
|
|
|
|
|
|
{QStringLiteral("windowactivate"), QStringLiteral("--sync"),
|
|
|
|
|
|
windowId}) == 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MoonlightSession::start(const ConnectionTicket& ticket, EventCallback callback) {
|
2026-10-28 08:48:12 -04:00
|
|
|
|
if (isRunning()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2026-10-30 02:42:11 -04:00
|
|
|
|
|
2026-10-28 08:48:12 -04:00
|
|
|
|
callback_ = std::move(callback);
|
2026-10-30 02:42:11 -04:00
|
|
|
|
stopRequested_ = false;
|
|
|
|
|
|
terminalEventDelivered_ = false;
|
|
|
|
|
|
readyEventDelivered_ = false;
|
|
|
|
|
|
outputParser_.reset();
|
|
|
|
|
|
readyTimer_->stop();
|
|
|
|
|
|
killTimer_->stop();
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
process_->setProgram(QStringLiteral("moonlight-qt"));
|
2026-10-30 02:42:11 -04:00
|
|
|
|
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
|
|
|
|
|
|
environment.insert(QStringLiteral("GL_IS_SLOW"), QStringLiteral("0"));
|
|
|
|
|
|
process_->setProcessEnvironment(environment);
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
|
args << QStringLiteral("stream")
|
|
|
|
|
|
<< ticket.endpoint
|
|
|
|
|
|
<< ticket.app
|
|
|
|
|
|
<< QStringLiteral("--resolution")
|
|
|
|
|
|
<< QStringLiteral("%1x%2").arg(ticket.width).arg(ticket.height)
|
|
|
|
|
|
<< QStringLiteral("--fps") << QString::number(ticket.fps)
|
|
|
|
|
|
<< QStringLiteral("--bitrate") << QString::number(20000)
|
|
|
|
|
|
<< QStringLiteral("--display-mode") << QStringLiteral("fullscreen")
|
|
|
|
|
|
<< QStringLiteral("--video-codec") << options_.videoCodec
|
|
|
|
|
|
<< QStringLiteral("--video-decoder") << options_.decoder
|
|
|
|
|
|
<< QStringLiteral("--absolute-mouse")
|
|
|
|
|
|
<< QStringLiteral("--keep-awake");
|
2026-10-30 02:42:11 -04:00
|
|
|
|
args << (options_.vsync ? QStringLiteral("--vsync") : QStringLiteral("--no-vsync"));
|
|
|
|
|
|
args << (options_.framePacing ? QStringLiteral("--frame-pacing")
|
|
|
|
|
|
: QStringLiteral("--no-frame-pacing"));
|
2026-10-28 08:48:12 -04:00
|
|
|
|
if (options_.performanceOverlay) {
|
|
|
|
|
|
args << QStringLiteral("--performance-overlay");
|
|
|
|
|
|
}
|
2026-10-30 02:42:11 -04:00
|
|
|
|
args << (options_.audioOnHost ? QStringLiteral("--audio-on-host")
|
|
|
|
|
|
: QStringLiteral("--no-audio-on-host"));
|
2026-10-28 08:48:12 -04:00
|
|
|
|
process_->setArguments(args);
|
|
|
|
|
|
|
2026-10-30 02:42:11 -04:00
|
|
|
|
const QString logDir = QDir::homePath() + QStringLiteral("/vdi-terminal/logs");
|
2026-10-28 08:48:12 -04:00
|
|
|
|
QDir().mkpath(logDir);
|
2026-10-30 02:42:11 -04:00
|
|
|
|
logPath_ = logDir + QStringLiteral("/moonlight-%1.log")
|
|
|
|
|
|
.arg(QDate::currentDate().toString(QStringLiteral("yyyyMMdd")));
|
2026-10-28 08:48:12 -04:00
|
|
|
|
|
|
|
|
|
|
process_->start();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 02:42:11 -04:00
|
|
|
|
void MoonlightSession::onProcessStarted() {
|
|
|
|
|
|
emitEvent(EventType::kStarted, QStringLiteral("串流进程已启动"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 08:48:12 -04:00
|
|
|
|
void MoonlightSession::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
readyTimer_->stop();
|
|
|
|
|
|
killTimer_->stop();
|
|
|
|
|
|
|
|
|
|
|
|
if (stopRequested_) {
|
|
|
|
|
|
emitTerminalEvent(EventType::kFinished, QStringLiteral("串流已结束"));
|
|
|
|
|
|
return;
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
2026-10-30 02:42:11 -04:00
|
|
|
|
|
|
|
|
|
|
const QString message = exitStatus == QProcess::CrashExit
|
|
|
|
|
|
? QStringLiteral("串流异常退出 (code=%1)").arg(exitCode)
|
|
|
|
|
|
: QStringLiteral("串流已结束 (code=%1)").arg(exitCode);
|
|
|
|
|
|
emitTerminalEvent(exitStatus == QProcess::CrashExit ? EventType::kFailed
|
|
|
|
|
|
: EventType::kFinished,
|
|
|
|
|
|
message);
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::onProcessError(QProcess::ProcessError error) {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
if (error == QProcess::Crashed) {
|
|
|
|
|
|
return;
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
2026-10-30 02:42:11 -04:00
|
|
|
|
|
|
|
|
|
|
const QString message = error == QProcess::FailedToStart
|
|
|
|
|
|
? QStringLiteral("无法启动 moonlight-qt,请检查安装")
|
|
|
|
|
|
: QStringLiteral("串流进程错误 (%1)").arg(static_cast<int>(error));
|
|
|
|
|
|
emitTerminalEvent(EventType::kFailed, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::onProcessOutput() {
|
|
|
|
|
|
const QByteArray output = process_->readAllStandardOutput();
|
|
|
|
|
|
if (output.isEmpty()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QFile log(logPath_);
|
|
|
|
|
|
if (log.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
|
|
|
|
|
|
log.write(output);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool wasReady = outputParser_.isReady();
|
|
|
|
|
|
outputParser_.append(output);
|
|
|
|
|
|
if (outputParser_.hasTerminalError()) {
|
|
|
|
|
|
readyTimer_->stop();
|
|
|
|
|
|
emitTerminalEvent(EventType::kFailed, QStringLiteral("云桌面连接已中断"));
|
|
|
|
|
|
stop();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!wasReady && outputParser_.isReady()) {
|
|
|
|
|
|
scheduleReadyEvent();
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::stop() {
|
2026-10-30 02:42:11 -04:00
|
|
|
|
if (!isRunning()) {
|
2026-10-28 08:48:12 -04:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-10-30 02:42:11 -04:00
|
|
|
|
stopRequested_ = true;
|
|
|
|
|
|
readyTimer_->stop();
|
|
|
|
|
|
process_->terminate();
|
|
|
|
|
|
killTimer_->start(kTerminateGraceMs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::emitEvent(EventType type, const QString& message) {
|
|
|
|
|
|
if (callback_) {
|
|
|
|
|
|
callback_(Event{type, message});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::emitTerminalEvent(EventType type, const QString& message) {
|
|
|
|
|
|
if (terminalEventDelivered_) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
terminalEventDelivered_ = true;
|
|
|
|
|
|
emitEvent(type, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MoonlightSession::scheduleReadyEvent() {
|
|
|
|
|
|
if (!readyEventDelivered_ && !terminalEventDelivered_ && isRunning()) {
|
|
|
|
|
|
readyTimer_->start(kReadyStableMs);
|
2026-10-28 08:48:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace vdi
|