Skip to content

Commit

Permalink
Merge pull request #600 from barry-ran/dev
Browse files Browse the repository at this point in the history
feat: adjust ui
  • Loading branch information
barry-ran authored Apr 9, 2022
2 parents 44d2825 + 816ae74 commit bb43261
Show file tree
Hide file tree
Showing 30 changed files with 1,174 additions and 939 deletions.
24 changes: 13 additions & 11 deletions QtScrcpy/device/controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "receiver.h"
#include "videosocket.h"

Controller::Controller(QString gameScript, QObject *parent) : QObject(parent)
Controller::Controller(std::function<qint64(const QByteArray&)> sendData, QString gameScript, QObject *parent)
: QObject(parent)
, m_sendData(sendData)
{
m_receiver = new Receiver(this);
Q_ASSERT(m_receiver);
Expand All @@ -17,20 +19,20 @@ Controller::Controller(QString gameScript, QObject *parent) : QObject(parent)

Controller::~Controller() {}

void Controller::setControlSocket(QTcpSocket *controlSocket)
void Controller::postControlMsg(ControlMsg *controlMsg)
{
if (m_controlSocket || !controlSocket) {
return;
if (controlMsg) {
QCoreApplication::postEvent(this, controlMsg);
}
m_controlSocket = controlSocket;
m_receiver->setControlSocket(controlSocket);
}

void Controller::postControlMsg(ControlMsg *controlMsg)
void Controller::recvDeviceMsg(DeviceMsg *deviceMsg)
{
if (controlMsg) {
QCoreApplication::postEvent(this, controlMsg);
if (!m_receiver) {
return;
}

m_receiver->recvDeviceMsg(deviceMsg);
}

void Controller::test(QRect rc)
Expand Down Expand Up @@ -236,8 +238,8 @@ bool Controller::sendControl(const QByteArray &buffer)
return false;
}
qint32 len = 0;
if (m_controlSocket) {
len = static_cast<qint32>(m_controlSocket->write(buffer.data(), buffer.length()));
if (m_sendData) {
len = static_cast<qint32>(m_sendData(buffer));
}
return len == buffer.length() ? true : false;
}
Expand Down
8 changes: 5 additions & 3 deletions QtScrcpy/device/controller/controller.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#ifndef CONTROLLER_H
#define CONTROLLER_H

Expand All @@ -9,15 +10,16 @@
class QTcpSocket;
class Receiver;
class InputConvertBase;
class DeviceMsg;
class Controller : public QObject
{
Q_OBJECT
public:
Controller(QString gameScript = "", QObject *parent = Q_NULLPTR);
Controller(std::function<qint64(const QByteArray&)> sendData, QString gameScript = "", QObject *parent = Q_NULLPTR);
virtual ~Controller();

void setControlSocket(QTcpSocket *controlSocket);
void postControlMsg(ControlMsg *controlMsg);
void recvDeviceMsg(DeviceMsg *deviceMsg);
void test(QRect rc);

void updateScript(QString gameScript = "");
Expand Down Expand Up @@ -62,9 +64,9 @@ public slots:
void postKeyCodeClick(AndroidKeycode keycode);

private:
QPointer<QTcpSocket> m_controlSocket;
QPointer<Receiver> m_receiver;
QPointer<InputConvertBase> m_inputConvert;
std::function<qint64(const QByteArray&)> m_sendData = Q_NULLPTR;
};

#endif // CONTROLLER_H
30 changes: 1 addition & 29 deletions QtScrcpy/device/controller/receiver/receiver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <QApplication>
#include <QClipboard>
#include <QTcpSocket>

#include "devicemsg.h"
#include "receiver.h"
Expand All @@ -9,34 +8,7 @@ Receiver::Receiver(QObject *parent) : QObject(parent) {}

Receiver::~Receiver() {}

void Receiver::setControlSocket(QTcpSocket *controlSocket)
{
if (m_controlSocket || !controlSocket) {
return;
}
m_controlSocket = controlSocket;
connect(controlSocket, &QTcpSocket::readyRead, this, &Receiver::onReadyRead);
}

void Receiver::onReadyRead()
{
if (!m_controlSocket) {
return;
}

while (m_controlSocket->bytesAvailable()) {
QByteArray byteArray = m_controlSocket->peek(m_controlSocket->bytesAvailable());
DeviceMsg deviceMsg;
qint32 consume = deviceMsg.deserialize(byteArray);
if (0 >= consume) {
break;
}
m_controlSocket->read(consume);
processMsg(&deviceMsg);
}
}

void Receiver::processMsg(DeviceMsg *deviceMsg)
void Receiver::recvDeviceMsg(DeviceMsg *deviceMsg)
{
switch (deviceMsg->type()) {
case DeviceMsg::DMT_GET_CLIPBOARD: {
Expand Down
12 changes: 1 addition & 11 deletions QtScrcpy/device/controller/receiver/receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <QPointer>

class QTcpSocket;
class DeviceMsg;
class Receiver : public QObject
{
Expand All @@ -12,16 +11,7 @@ class Receiver : public QObject
explicit Receiver(QObject *parent = Q_NULLPTR);
virtual ~Receiver();

void setControlSocket(QTcpSocket *controlSocket);

public slots:
void onReadyRead();

protected:
void processMsg(DeviceMsg *deviceMsg);

private:
QPointer<QTcpSocket> m_controlSocket;
void recvDeviceMsg(DeviceMsg *deviceMsg);
};

#endif // RECEIVER_H
49 changes: 42 additions & 7 deletions QtScrcpy/device/decoder/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@
#include "decoder.h"
#include "videobuffer.h"

Decoder::Decoder(VideoBuffer *vb, QObject *parent) : QObject(parent), m_vb(vb) {}
Decoder::Decoder(std::function<void(int, int, uint8_t*, uint8_t*, uint8_t*, int, int, int)> onFrame, QObject *parent)
: QObject(parent)
, m_vb(new VideoBuffer())
, m_onFrame(onFrame)
{
m_vb->init();
connect(this, &Decoder::newFrame, this, &Decoder::onNewFrame, Qt::QueuedConnection);
connect(m_vb, &VideoBuffer::updateFPS, this, &Decoder::updateFPS);
}

Decoder::~Decoder() {}
Decoder::~Decoder() {
m_vb->deInit();
delete m_vb;
}

bool Decoder::open(const AVCodec *codec)
bool Decoder::open()
{
// codec
AVCodec *codec = Q_NULLPTR;
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
qCritical("H.264 decoder not found");
return false;
}

// codec context
m_codecCtx = avcodec_alloc_context3(codec);
if (!m_codecCtx) {
Expand All @@ -26,6 +45,10 @@ bool Decoder::open(const AVCodec *codec)

void Decoder::close()
{
if (m_vb) {
m_vb->interrupt();
}

if (!m_codecCtx) {
return;
}
Expand Down Expand Up @@ -98,11 +121,12 @@ bool Decoder::push(const AVPacket *packet)
return true;
}

void Decoder::interrupt()
void Decoder::peekFrame(std::function<void (int, int, uint8_t *)> onFrame)
{
if (m_vb) {
m_vb->interrupt();
if (!m_vb) {
return;
}
m_vb->peekRenderedFrame(onFrame);
}

void Decoder::pushFrame()
Expand All @@ -116,5 +140,16 @@ void Decoder::pushFrame()
// the previous newFrame will consume this frame
return;
}
emit onNewFrame();
emit newFrame();
}

void Decoder::onNewFrame() {
if (!m_onFrame) {
return;
}

m_vb->lock();
const AVFrame *frame = m_vb->consumeRenderedFrame();
m_onFrame(frame->width, frame->height, frame->data[0], frame->data[1], frame->data[2], frame->linesize[0], frame->linesize[1], frame->linesize[2]);
m_vb->unLock();
}
15 changes: 11 additions & 4 deletions QtScrcpy/device/decoder/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,31 @@ class Decoder : public QObject
{
Q_OBJECT
public:
Decoder(VideoBuffer *vb, QObject *parent = Q_NULLPTR);
Decoder(std::function<void(int width, int height, uint8_t* dataY, uint8_t* dataU, uint8_t* dataV, int linesizeY, int linesizeU, int linesizeV)> onFrame, QObject *parent = Q_NULLPTR);
virtual ~Decoder();

bool open(const AVCodec *codec);
bool open();
void close();
bool push(const AVPacket *packet);
void interrupt();
void peekFrame(std::function<void(int width, int height, uint8_t* dataRGB32)> onFrame);

signals:
void updateFPS(quint32 fps);

private slots:
void onNewFrame();

protected:
signals:
void newFrame();

private:
void pushFrame();

private:
VideoBuffer *m_vb = Q_NULLPTR;
AVCodecContext *m_codecCtx = Q_NULLPTR;
bool m_isCodecCtxOpen = false;
std::function<void(int, int, uint8_t*, uint8_t*, uint8_t*, int, int, int)> m_onFrame = Q_NULLPTR;
};

#endif // DECODER_H
65 changes: 55 additions & 10 deletions QtScrcpy/device/decoder/videobuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include "videobuffer.h"
#include "avframeconvert.h"
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/imgutils.h"
}

VideoBuffer::VideoBuffer() {}
VideoBuffer::VideoBuffer(QObject *parent) : QObject(parent) {
connect(&m_fpsCounter, &FpsCounter::updateFPS, this, &VideoBuffer::updateFPS);
}

VideoBuffer::~VideoBuffer() {}

bool VideoBuffer::init(bool renderExpiredFrames)
bool VideoBuffer::init()
{
m_renderExpiredFrames = renderExpiredFrames;
m_decodingFrame = av_frame_alloc();
if (!m_decodingFrame) {
goto error;
Expand Down Expand Up @@ -57,6 +60,11 @@ void VideoBuffer::unLock()
m_mutex.unlock();
}

void VideoBuffer::setRenderExpiredFrames(bool renderExpiredFrames)
{
m_renderExpiredFrames = renderExpiredFrames;
}

AVFrame *VideoBuffer::decodingFrame()
{
return m_decodingFrame;
Expand Down Expand Up @@ -99,9 +107,51 @@ const AVFrame *VideoBuffer::consumeRenderedFrame()
return m_renderingframe;
}

const AVFrame *VideoBuffer::peekRenderedFrame()
void VideoBuffer::peekRenderedFrame(std::function<void(int width, int height, uint8_t* dataRGB32)> onFrame)
{
return m_renderingframe;
if (!onFrame) {
return;
}

lock();
auto frame = m_renderingframe;
int width = frame->width;
int height = frame->height;

// create buffer
uint8_t* rgbBuffer = new uint8_t[width * height * 4];
AVFrame *rgbFrame = av_frame_alloc();
if (!rgbFrame) {
delete [] rgbBuffer;
return;
}

// bind buffer to AVFrame
av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, rgbBuffer, AV_PIX_FMT_RGB32, width, height, 4);

// convert
AVFrameConvert convert;
convert.setSrcFrameInfo(width, height, AV_PIX_FMT_YUV420P);
convert.setDstFrameInfo(width, height, AV_PIX_FMT_RGB32);
bool ret = false;
ret = convert.init();
if (!ret) {
delete [] rgbBuffer;
av_free(rgbFrame);
return;
}
ret = convert.convert(frame, rgbFrame);
if (!ret) {
delete [] rgbBuffer;
av_free(rgbFrame);
return;
}
convert.deInit();
av_free(rgbFrame);
unLock();

onFrame(width, height, rgbBuffer);
delete [] rgbBuffer;
}

void VideoBuffer::interrupt()
Expand All @@ -115,11 +165,6 @@ void VideoBuffer::interrupt()
}
}

FpsCounter *VideoBuffer::getFPSCounter()
{
return &m_fpsCounter;
}

void VideoBuffer::swap()
{
AVFrame *tmp = m_decodingFrame;
Expand Down
Loading

0 comments on commit bb43261

Please sign in to comment.