Skip to content

Commit

Permalink
feat: Adapt to qt6
Browse files Browse the repository at this point in the history
  • Loading branch information
kaqduz committed Jan 8, 2025
1 parent d444f26 commit bdc543d
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 186 deletions.
13 changes: 8 additions & 5 deletions QtScrcpy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Network Multimedia REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Network Multimedia REQUIRED)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Network Multimedia OpenGL OpenGLWidgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Network Multimedia OpenGL OpenGLWidgets REQUIRED)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(QT NAMES Qt6 Qt5 COMPONENTS X11Extras REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS X11Extras REQUIRED)
find_package(X11 REQUIRED)
endif()

message(STATUS "[${PROJECT_NAME}] Qt version is: ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}")
Expand Down Expand Up @@ -310,13 +309,15 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include_directories(${X11_INCLUDE_DIR})

target_link_libraries(${PROJECT_NAME} PRIVATE
# qx11
Qt${QT_VERSION_MAJOR}::X11Extras
Qt${QT_VERSION_MAJOR}::CorePrivate
# xcb https://doc.qt.io/qt-5/linux-requirements.html
xcb
# pthread
${X11_LIBRARIES}
Threads::Threads
)

Expand All @@ -334,5 +335,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Network
Qt${QT_VERSION_MAJOR}::Multimedia
Qt${QT_VERSION_MAJOR}::OpenGL
Qt${QT_VERSION_MAJOR}::OpenGLWidgets
QtScrcpyCore
)
84 changes: 38 additions & 46 deletions QtScrcpy/audio/audiooutput.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
#include <QTcpSocket>
#include <QHostAddress>
#include <QAudioOutput>
#include <QTime>
#include <QAudioSink>
#include <QMediaDevices>
#include <QElapsedTimer>

#include <QProcess>
#include <QThread>
#include <QDebug>
#include <QByteArray>
#include "audiooutput.h"

AudioOutput::AudioOutput(QObject *parent)
: QObject(parent)
, m_outputDevice(nullptr)
, m_running(false)
, m_audioSink(nullptr)
{
connect(&m_sndcpy, &QProcess::readyReadStandardOutput, this, [this]() {
qInfo() << QString("AudioOutput::") << QString(m_sndcpy.readAllStandardOutput());
qInfo() << QString("AudioOutput::") << m_sndcpy.readAllStandardOutput();
});
connect(&m_sndcpy, &QProcess::readyReadStandardError, this, [this]() {
qInfo() << QString("AudioOutput::") << QString(m_sndcpy.readAllStandardError());
qInfo() << QString("AudioOutput::") << m_sndcpy.readAllStandardError();
});
}

AudioOutput::~AudioOutput()
{
if (QProcess::NotRunning != m_sndcpy.state()) {
if (m_sndcpy.state() != QProcess::NotRunning) {
m_sndcpy.kill();
}
stop();
}

bool AudioOutput::start(const QString& serial, int port)
bool AudioOutput::start(const QString &serial, int port)
{
if (m_running) {
stop();
Expand All @@ -36,7 +42,7 @@ bool AudioOutput::start(const QString& serial, int port)
bool ret = runSndcpyProcess(serial, port);
qInfo() << "AudioOutput::run sndcpy cost:" << timeConsumeCount.elapsed() << "milliseconds";
if (!ret) {
return ret;
return false;
}

startAudioOutput();
Expand Down Expand Up @@ -64,20 +70,15 @@ void AudioOutput::installonly(const QString &serial, int port)

bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait)
{
if (QProcess::NotRunning != m_sndcpy.state()) {
if (m_sndcpy.state() != QProcess::NotRunning) {
m_sndcpy.kill();
}

#ifdef Q_OS_WIN32
QStringList params;
params << serial;
params << QString("%1").arg(port);
QStringList params{serial, QString::number(port)};
m_sndcpy.start("sndcpy.bat", params);
#else
QStringList params;
params << "sndcpy.sh";
params << serial;
params << QString("%1").arg(port);
QStringList params{"sndcpy.sh", serial, QString::number(port)};
m_sndcpy.start("bash", params);
#endif

Expand All @@ -86,11 +87,11 @@ bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait)
}

if (!m_sndcpy.waitForStarted()) {
qWarning() << "AudioOutput::start sndcpy.bat failed";
qWarning() << "AudioOutput::start sndcpy process failed";
return false;
}
if (!m_sndcpy.waitForFinished()) {
qWarning() << "AudioOutput::sndcpy.bat crashed";
qWarning() << "AudioOutput::sndcpy process crashed";
return false;
}

Expand All @@ -99,41 +100,39 @@ bool AudioOutput::runSndcpyProcess(const QString &serial, int port, bool wait)

void AudioOutput::startAudioOutput()
{
if (m_audioOutput) {
if (m_audioSink) {
return;
}

QAudioFormat format;
format.setSampleRate(48000);
format.setChannelCount(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
format.setSampleFormat(QAudioFormat::Int16); // 16-bit signed integer format

QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
QAudioDevice defaultDevice = QMediaDevices::defaultAudioOutput();
if (!defaultDevice.isFormatSupported(format)) {
qWarning() << "AudioOutput::audio format not supported, cannot play audio.";
return;
}

m_audioOutput = new QAudioOutput(format, this);
connect(m_audioOutput, &QAudioOutput::stateChanged, this, [](QAudio::State state) {
qInfo() << "AudioOutput::audio state changed:" << state;
});
m_audioOutput->setBufferSize(48000*2*15/1000 * 20);
m_outputDevice = m_audioOutput->start();
m_audioSink = new QAudioSink(defaultDevice, format, this);
m_outputDevice = m_audioSink->start();
if (!m_outputDevice) {
qWarning() << "AudioOutput::failed to start audio sink.";
delete m_audioSink;
m_audioSink = nullptr;
return;
}
}

void AudioOutput::stopAudioOutput()
{
if (!m_audioOutput) {
return;
if (m_audioSink) {
m_audioSink->stop();
delete m_audioSink;
m_audioSink = nullptr;
}

m_audioOutput->stop();
delete m_audioOutput;
m_audioOutput = nullptr;
m_outputDevice = nullptr;
}

void AudioOutput::startRecvData(int port)
Expand All @@ -156,7 +155,6 @@ void AudioOutput::startRecvData(int port)
});
connect(audioSocket, &QIODevice::readyRead, audioSocket, [this, audioSocket]() {
qint64 recv = audioSocket->bytesAvailable();
//qDebug() << "AudioOutput::recv data:" << recv;

if (!m_outputDevice) {
return;
Expand All @@ -165,22 +163,16 @@ void AudioOutput::startRecvData(int port)
m_buffer.reserve(recv);
}

qint64 count = audioSocket->read(m_buffer.data(), audioSocket->bytesAvailable());
qint64 count = audioSocket->read(m_buffer.data(), recv);
m_outputDevice->write(m_buffer.data(), count);
});
connect(audioSocket, &QTcpSocket::stateChanged, audioSocket, [](QAbstractSocket::SocketState state) {
qInfo() << "AudioOutput::audio socket state changed:" << state;

});
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)

connect(audioSocket, &QTcpSocket::errorOccurred, audioSocket, [](QAbstractSocket::SocketError error) {
qInfo() << "AudioOutput::audio socket error occurred:" << error;
});
#else
connect(audioSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), audioSocket, [](QAbstractSocket::SocketError error) {
qInfo() << "AudioOutput::audio socket error occurred:" << error;
});
#endif

m_workerThread.start();
emit connectTo(port);
Expand Down
2 changes: 2 additions & 0 deletions QtScrcpy/audio/audiooutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QPointer>
#include <QVector>

class QAudioSink;
class QAudioOutput;
class QIODevice;
class AudioOutput : public QObject
Expand Down Expand Up @@ -36,6 +37,7 @@ class AudioOutput : public QObject
QProcess m_sndcpy;
QVector<char> m_buffer;
bool m_running = false;
QAudioSink *m_audioSink = nullptr;
};

#endif // AUDIOOUTPUT_H
4 changes: 1 addition & 3 deletions QtScrcpy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "config.h"
#include "dialog.h"
#include "mousetap/mousetap.h"

static Dialog *g_mainDlg = Q_NULLPTR;
static QtMessageHandler g_oldMessageHandler = Q_NULLPTR;
Expand Down Expand Up @@ -55,7 +54,6 @@ int main(int argc, char *argv[])
QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
}

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

#if (QT_VERSION >= QT_VERSION_CHECK(5,14,0))
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
Expand Down Expand Up @@ -142,7 +140,7 @@ void installTranslator()
break;
}

translator.load(languagePath);
qInfo() << "Loading translation result =" << translator.load(languagePath);
qApp->installTranslator(&translator);
}

Expand Down
6 changes: 4 additions & 2 deletions QtScrcpy/ui/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,11 @@ void Dialog::on_usbConnectBtn_clicked()

int Dialog::findDeviceFromeSerialBox(bool wifi)
{
QRegExp regIP("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])\\b");
QRegularExpression regIP(R"(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\:(?:[0-9]|[1-9]\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$)");

for (int i = 0; i < ui->serialBox->count(); ++i) {
bool isWifi = regIP.exactMatch(ui->serialBox->itemText(i));
QRegularExpressionMatch match = regIP.match(ui->serialBox->itemText(i));
bool isWifi = match.hasMatch();
bool found = wifi ? isWifi : !isWifi;
if (found) {
return i;
Expand Down
4 changes: 2 additions & 2 deletions QtScrcpy/ui/toolform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void ToolForm::updateGroupControl()
void ToolForm::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
event->accept();
}
}
Expand All @@ -81,7 +81,7 @@ void ToolForm::mouseReleaseEvent(QMouseEvent *event)
void ToolForm::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition);
move(event->globalPosition().toPoint() - m_dragPosition);
event->accept();
}
}
Expand Down
Loading

0 comments on commit bdc543d

Please sign in to comment.