linux 下scrcpy 手机投屏到电脑,QT+ffmpeg 获取视频流,处理
1 安装 scrcpy
地址 https://github.com/Genymobile/scrcpy
转到 relese 下载
我这里下载的是linux系统 v2.3.1 版本 scrcpy-2.3.1.tar.gz
下载 scrcpy-server v2.3.1 版本 scrcpy-server-v2.3.1
解压scrcpy-2.3.1.tar.gz 到目录 scrcpy-2.3.1 安装:
./install_release.sh
打印 scrcpy 安装目录是 /usr/local/bin/scrcpy
scrcpy-server安装目录是 /usr/local/share/scrcpy/scrcpy-server
2 安装scrcpy-server
把 scrcpy-server-v2.3.1 复制到 路径 /usr/local/share/scrcpy/scrcpy-server
mv scrcpy-server-v2.3.1 /usr/local/share/scrcpy/scrcpy-server
3 手机打开wifi debug ,通过无线连接,保证电脑和手机在同一个局域网
连接方式有很多,可以通过有线方式,这里用无线方式举例
我的是华为手机,查看这篇文章 ADB无线调试——华为篇——鸿蒙系统 https://blog.csdn.net/ai_qh77/article/details/123383801
其它品牌手机自行查找方法
4 scrcpy 连接手机测试
wmx@wmx-ubuntu:~$ adb devices
List of devices attached
192.168.2.105:5555 device
说明我的手机已经 adb无线调试模式 连接到电脑
然后输入命令:
wmx@wmx-ubuntu:~$ scrcpy
scrcpy 2.3.1 <https://github.com/Genymobile/scrcpy>
INFO: ADB device found:
INFO: --> (tcpip) 192.168.2.105:5555 device NOH_AN01
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 0.6 MB/s (66007 bytes in 0.098s)
[server] INFO: Device: [HUAWEI] HUAWEI NOH-AN01 (Android 12)
INFO: Renderer: opengl
INFO: OpenGL version: 4.6.0 NVIDIA 535.86.10
INFO: Trilinear filtering enabled
INFO: Texture: 1152x2376
可以看到手机已经投屏到ubuntu 电脑
这种方式 scrcpy 默认使用SDL渲染界面,如果我们想自己处理每一帧手机投屏的图像,接下来步骤5
5 通过 v4l2 方式,连接到linux电脑,然后Qt +ffmpeg 读取视频帧处理
参考 文章 https://github.com/Genymobile/scrcpy/blob/master/doc/v4l2.md
安装模块
sudo apt install v4l2loopback-dkms
查看设备
wmx@wmx-ubuntu:~$ v4l2-ctl --list-devices
aoni webcam A20: aoni webcam A2 (usb-0000:00:14.0-10.2):
/dev/video0
/dev/video1
/dev/media0
创建v4l2设备
sudo modprobe v4l2loopback
再查看设备:
wmx@wmx-ubuntu:~$ v4l2-ctl --list-devices
Dummy video device (0x0000) (platform:v4l2loopback-000):
/dev/video2
aoni webcam A20: aoni webcam A2 (usb-0000:00:14.0-10.2):
/dev/video0
/dev/video1
/dev/media0
看到多一个 /dev/video2
,这个就是我们通过代码读取的设备
scrcpy 通过 v4l2方式启动,关联/dev/video2
scrcpy --v4l2-sink=/dev/video2
6 QT +ffmpeg 读取设备 /dev/video2
我这已经提前安装好ffmpeg,配置好 linux 下 pkg-config
testVideo3.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
ffmpegvideo.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
ffmpegvideo.h \
mainwindow.h
FORMS += \
mainwindow.ui
message("use system ffmpeg")
!android {
unix:!macx {
message(linux ffmpeg !!! )
CONFIG += link_pkgconfig
# PKGCONFIG += 库的pc文件名
PKGCONFIG += libavcodec libavfilter libavformat libswscale libavutil libswresample libavdevice libpostproc
}
}
# Default rules for deployment.
DESTDIR = $$PWD/app
ffmpegvideo.h
#ifndef FFMPEGVIDEO_H
#define FFMPEGVIDEO_H
#include <QImage>
#include <QWidget>
#include <QPaintEvent>
#include <QThread>
#include <QPainter>
#include <QDebug>
#include <string>
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/samplefmt.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavutil/parseutils.h>
#include <libavutil/mem.h>
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
}
using namespace std;
class FFmpegVideo : public QThread
{
Q_OBJECT
public:
explicit FFmpegVideo();
~FFmpegVideo();
void setUrl(QString url);
bool open_input_file();
protected:
void run();
signals:
void sendQImage(QImage);
private:
AVFormatContext *fmtCtx =NULL;
const AVCodec *videoCodec =NULL;
AVCodecContext *videoCodecCtx=NULL;
AVPacket *pkt = NULL;
AVFrame *yuvFrame = NULL;
AVFrame *rgbFrame = NULL;
struct SwsContext *img_ctx=NULL;
unsigned char *out_buffer=nullptr;
int videoStreamIndex =-1;
int numBytes = -1;
QString _url;
};
class FFmpegWidget : public QWidget
{
Q_OBJECT
public:
explicit FFmpegWidget(QWidget *parent = nullptr);
~FFmpegWidget(