Compare commits

..

No commits in common. 'master' and 'zpx' have entirely different histories.
master ... zpx

@ -13,14 +13,9 @@ qt_add_executable(VideoClient
videoControl.h videoControl.ui videoControl.cpp
wprogressbar.h wprogressbar.cpp
webmapwidget.h webmapwidget.cpp
joystickcontroller.h joystickcontroller.cpp
directinputjoystick.h directinputjoystick.cpp
)
qt6_add_resources(VideoClient "resources"
PREFIX "/"
FILES "resources/res.qrc"
@ -44,11 +39,7 @@ target_link_libraries(VideoClient
Qt6::Network
Qt6::SerialPort
Qt6::WebEngineWidgets
#Qt6::Gamepad
video
winmm
dinput8
dxguid
# QWindowKit::Widgets
# WidgetFrame
)

@ -145,69 +145,3 @@ void CommandWidget::on_pushButton_16_clicked() {
void CommandWidget::on_pushButton_15_clicked() {
buttonResponse(0xC9);
}
void CommandWidget::onButtonChanged(int button, bool pressed)
{
qDebug() << "Button" << button << (pressed ? "Pressed" : "Released");
// switch (button)
// {
// case 0:
// on_pushButton_clicked();
// break;
// case 1:
// on_pushButton_2_clicked();
// break;
// case 2:
// on_pushButton_3_clicked();
// break;
// case 3:
// on_pushButton_clicked();
// break;
// case 4:
// on_pushButton_2_clicked();
// break;
// case 5:
// on_pushButton_3_clicked();
// break;
// case 6:
// on_pushButton_clicked();
// break;
// case 7:
// on_pushButton_2_clicked();
// break;
// case 8:
// on_pushButton_3_clicked();
// break;
// case 9:
// on_pushButton_clicked();
// break;
// case 10:
// on_pushButton_2_clicked();
// break;
// case 11:
// on_pushButton_3_clicked();
// break;
// case 12:
// on_pushButton_clicked();
// break;
// case 13:
// on_pushButton_2_clicked();
// break;
// case 14:
// on_pushButton_3_clicked();
// break;
// }
}
void CommandWidget::onDirectionChanged(int direction){
direction!=42949672?qDebug() << "direction" << direction:qDebug() << "center";
}

@ -122,12 +122,6 @@ private slots:
void on_pushButton_15_clicked();
public slots:
void onButtonChanged(int button, bool pressed);
void onDirectionChanged(int dirction);
private:
Ui::CommandWidget *ui;
QUdpSocket *m_commandUdpSocket = nullptr;

@ -1,164 +0,0 @@
#include "DirectInputJoystick.h"
#include <QDebug>
DirectInputJoystick::DirectInputJoystick(QObject *parent) : QObject(parent) {
// 确保 COM 库在主线程初始化
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
}
DirectInputJoystick::~DirectInputJoystick() {
cleanup();
CoUninitialize();
}
bool DirectInputJoystick::initialize() {
HRESULT hr = DirectInput8Create(
GetModuleHandle(nullptr),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(VOID**)&m_pDirectInput,
nullptr
);
if (FAILED(hr)) {
emit errorOccurred("无法创建 DirectInput 对象");
return false;
}
// 枚举所有摇杆设备
hr = m_pDirectInput->EnumDevices(
DI8DEVCLASS_GAMECTRL,
enumJoysticksCallback,
this,
DIEDFL_ATTACHEDONLY
);
return SUCCEEDED(hr);
}
BOOL CALLBACK DirectInputJoystick::enumJoysticksCallback(const DIDEVICEINSTANCE* instance, VOID* context) {
DirectInputJoystick* joystick = static_cast<DirectInputJoystick*>(context);
return joystick->setupDevice();
}
bool DirectInputJoystick::setupDevice() {
if (!m_pDirectInput) return false;
// 创建设备
HRESULT hr = m_pDirectInput->CreateDevice(
GUID_Joystick,
&m_pJoystick,
nullptr
);
if (FAILED(hr)) {
emit errorOccurred("无法创建摇杆设备");
return false;
}
// 设置数据格式
hr = m_pJoystick->SetDataFormat(&c_dfDIJoystick);
if (FAILED(hr)) {
emit errorOccurred("设置数据格式失败");
return false;
}
// 设置协作级别
hr = m_pJoystick->SetCooperativeLevel(
nullptr, // 无窗口句柄
DISCL_BACKGROUND | DISCL_NONEXCLUSIVE
);
// 枚举轴并设置范围
hr = m_pJoystick->EnumObjects(enumAxesCallback, this, DIDFT_AXIS);
if (FAILED(hr)) {
emit errorOccurred("枚举摇杆轴失败");
return false;
}
// 创建事件对象
m_hEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
hr = m_pJoystick->SetEventNotification(m_hEvent);
if (FAILED(hr)) {
emit errorOccurred("设置事件通知失败");
return false;
}
return true;
}
BOOL CALLBACK DirectInputJoystick::enumAxesCallback(const DIDEVICEOBJECTINSTANCE* doi, VOID* context) {
DirectInputJoystick* joystick = static_cast<DirectInputJoystick*>(context);
DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = doi->dwType;
diprg.lMin = -1000;
diprg.lMax = 1000;
HRESULT hr = joystick->m_pJoystick->SetProperty(DIPROP_RANGE, &diprg.diph);
return SUCCEEDED(hr);
}
void DirectInputJoystick::startListening() {
if (!m_pJoystick || m_isRunning) return;
m_isRunning = true;
m_workerThread = QThread::create([this]() {
m_pJoystick->Acquire();
while (m_isRunning) {
WaitForSingleObject(m_hEvent, INFINITE);
DIJOYSTATE state;
HRESULT hr = m_pJoystick->GetDeviceState(sizeof(DIJOYSTATE), &state);
if (SUCCEEDED(hr)) {
// 处理轴数据
double x = state.lX / 1000.0;
double y = state.lY / 1000.0;
double z = state.lZ / 1000.0;
emit axisChanged(0, x);
emit axisChanged(1, y);
emit axisChanged(2, z);
// 处理按钮
for (int i = 0; i < 32; ++i) {
if(lastState.rgbButtons[i]!=state.rgbButtons[i])
{
bool pressed = (state.rgbButtons[i] & 0x80) != 0;
emit buttonChanged(i, pressed);
}
}
if(lastState.rgdwPOV[0]!=state.rgdwPOV[0])
{
emit directionChanged(state.rgdwPOV[0]/100);
}
}
lastState = state;
}
});
connect(m_workerThread, &QThread::finished, m_workerThread, &QThread::deleteLater);
m_workerThread->start();
}
void DirectInputJoystick::cleanup() {
m_isRunning = false;
if (m_hEvent) {
CloseHandle(m_hEvent);
m_hEvent = nullptr;
}
if (m_pJoystick) {
m_pJoystick->Unacquire();
m_pJoystick->Release();
m_pJoystick = nullptr;
}
if (m_pDirectInput) {
m_pDirectInput->Release();
m_pDirectInput = nullptr;
}
}

@ -1,47 +0,0 @@
#ifndef DIRECTINPUTJOYSTICK_H
#define DIRECTINPUTJOYSTICK_H
#include <QObject>
#include <windows.h>
#include <dinput.h>
#include <QThread>
class DirectInputJoystick : public QObject
{
Q_OBJECT
public:
explicit DirectInputJoystick(QObject *parent = nullptr);
~DirectInputJoystick();
bool initialize();
void startListening();
signals:
// 轴变化信号axis: 0-X, 1-Y, 2-Z; value: -1.0 ~ 1.0
void axisChanged(int axis, double value);
// 按钮状态变化button: 按钮编号从0开始pressed: 是否按下)
void buttonChanged(int button, bool pressed);
// 方向按钮状态变化direction: 按钮编号上下左右pressed: 是否按下)
void directionChanged(int direction);
// 错误信号
void errorOccurred(const QString &message);
private:
bool setupDevice();
void cleanup();
static BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* doi, VOID* context);
static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE* instance, VOID* context);
DIJOYSTATE lastState;
LPDIRECTINPUT8 m_pDirectInput = nullptr;
LPDIRECTINPUTDEVICE8 m_pJoystick = nullptr;
HANDLE m_hEvent = nullptr;
QThread *m_workerThread;
bool m_isRunning = false;
};
#endif // DIRECTINPUTJOYSTICK_H

@ -1,106 +0,0 @@
#include "joystickcontroller.h"
#include <QDebug>
JoystickController::JoystickController(int joystickId, QObject *parent)
: QObject{parent}, m_joystickId(joystickId), m_isConnected(false)
{
// 初始化定时器
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &JoystickController::pollJoystick);
// 检测设备是否存在
UINT numDevs = joyGetNumDevs();
if (numDevs == 0) {
emit errorOccurred("系统未检测到任何摇杆设备");
return;
}
// 获取设备能力
MMRESULT capsResult = joyGetDevCaps(m_joystickId, &m_joyCaps, sizeof(JOYCAPS));
if (capsResult != JOYERR_NOERROR) {
emit errorOccurred(QString("无法获取摇杆设备能力,错误代码: %1").arg(capsResult));
return;
}
m_isConnected = true;
qDebug() << "摇杆设备初始化成功,名称:" << QString::fromWCharArray(m_joyCaps.szPname);
}
JoystickController::~JoystickController()
{
stopPolling();
}
bool JoystickController::isConnected() const
{
return m_isConnected;
}
void JoystickController::startPolling(int intervalMs)
{
if (m_isConnected) {
m_timer->start(intervalMs);
}
}
void JoystickController::stopPolling()
{
m_timer->stop();
}
void JoystickController::pollJoystick()
{
JOYINFOEX joyInfo;
joyInfo.dwSize = sizeof(JOYINFOEX);
joyInfo.dwFlags = JOY_RETURNALL;
MMRESULT result = joyGetPosEx(m_joystickId, &joyInfo);
if (result != JOYERR_NOERROR) {
emit errorOccurred(QString("读取摇杆数据失败,错误代码: %1").arg(result));
m_isConnected = false;
stopPolling();
return;
}
// 处理轴数据
normalizeAxisValues(joyInfo);
// 处理按钮状态
for (int i = 0; i < m_joyCaps.wNumButtons; ++i) {
bool isPressed = (joyInfo.dwButtons & (1 << i)) != 0;
emit buttonChanged(i + 1, isPressed); // 按钮ID从1开始
}
}
void JoystickController::normalizeAxisValues(const JOYINFOEX &joyInfo)
{
// X/Y轴归一化处理
double x = (joyInfo.dwXpos - 32768) / 32768.0;
double y = (joyInfo.dwYpos - 32768) / 32768.0;
emit axisChanged(1, x); // 假设轴1为X轴
emit axisChanged(2, y); // 轴2为Y轴
// 其他轴如Z轴、Rudder等
if (m_joyCaps.wCaps & JOYCAPS_HASZ) {
double z = (joyInfo.dwZpos - 32768) / 32768.0;
emit axisChanged(3, z);
}
}

@ -1,41 +0,0 @@
#ifndef JOYSTICKCONTROLLER_H
#define JOYSTICKCONTROLLER_H
#include <QObject>
#include <QTimer>
#include <windows.h>
#include <mmsystem.h>
class JoystickController : public QObject
{
Q_OBJECT
public:
explicit JoystickController(int joystickId = JOYSTICKID1, QObject *parent = nullptr);
~JoystickController();
bool isConnected() const;
void startPolling(int intervalMs = 50);
void stopPolling();
signals:
// 摇杆轴变化信号(范围:-1.0 ~ 1.0
void axisChanged(int axis, double value);
// 按钮状态变化信号按钮ID, 是否按下)
void buttonChanged(int button, bool pressed);
// 错误信号
void errorOccurred(const QString &message);
private slots:
void pollJoystick();
private:
void normalizeAxisValues(const JOYINFOEX &joyInfo);
QTimer *m_timer;
int m_joystickId;
bool m_isConnected;
JOYCAPS m_joyCaps;
};
#endif // JOYSTICKCONTROLLER_H

@ -32,7 +32,6 @@ MainWindow::MainWindow(QWidget *parent)
qDebug() << "Open file fail " << Qt::endl;
}
// QList<QString> list;
// list.append("rtmp://liteavapp.qcloud.com/live/liteavdemoplayerstreamid");
// list.append("rtsp://182.92.130.23/nmyj/video");
@ -61,8 +60,6 @@ MainWindow::MainWindow(QWidget *parent)
initHideButton();
//initJoyStick();
initDtJoyStick();
setSavedVideoDir();
// ui->stackedWidget->setCurrentIndex(1);
// ui->videoWidget1->play(list.at(0));
@ -126,8 +123,6 @@ void MainWindow::initSignalConnection() {
connect(ui->videoControlWidget, &VideoControl::stopVideoSignal, this,
&MainWindow::closeVideoSlot);
}
void MainWindow::initNotifyMessageConnection() {
@ -146,38 +141,6 @@ void MainWindow::initChangeVideoLayoutConnection() {
});
}
void MainWindow::onAxisChanged(int axis, double value)
{
qDebug() << "Axis" << axis << "Value:" << value;
}
// void MainWindow::initJoyStick()
// {
// m_joystick = new JoystickController(JOYSTICKID1,this);
// connect(m_joystick, &JoystickController::axisChanged, this, &MainWindow::onAxisChanged);
// connect(m_joystick, &JoystickController::buttonChanged, this, &MainWindow::onButtonChanged);
// if (m_joystick->isConnected()) {
// m_joystick->startPolling();
// }
// }
void MainWindow::initDtJoyStick()
{
m_dtjoystick = new DirectInputJoystick(this);
if (m_dtjoystick->initialize()) {
connect(m_dtjoystick, &DirectInputJoystick::buttonChanged, ui->commandWidget, &CommandWidget::onButtonChanged);
connect(m_dtjoystick, &DirectInputJoystick::directionChanged, ui->commandWidget, &CommandWidget::onDirectionChanged);
// connect(m_dtjoystick, &DirectInputJoystick::buttonChanged, this, &MainWindow::onButtonChanged);
m_dtjoystick->startListening();
}
}
void MainWindow::initNotifyManager() {
m_notifyManager = new NotifyManager(this, this);
m_notifyManager->setMaxCount(5);
@ -505,10 +468,7 @@ void MainWindow::pushStreamSlot(bool bPush) {
} else {
pushStreamIP = g_pushStreamInfoStruct.pushStreamIP;
}
QStringList pushStreamIPs;
pushStreamIPs.append(pushStreamIP);
// pushStreamIPs.append("rtmp://182.92.130.23/videoclient/fp98C_1_gcs");
ui->videoWidget->pushStream(pushStreamIPs);
ui->videoWidget->pushStream(pushStreamIP);
} else {
ui->videoWidget->stopPushStream();
// ui->videoWidget1->stopPushStream();

@ -16,9 +16,7 @@
#include "videoControl.h"
#include "videowidget.h"
#include "webmapwidget.h"
//#include "joystickapi.h"
#include "joystickcontroller.h"
#include "DirectInputJoystick.h"
#ifdef Q_OS_WIN
#include "windows.h"
#include "windowsx.h"
@ -70,14 +68,6 @@ private:
void setSavedVideoDir();
void initHideButton();
// void initJoyStick();
void initDtJoyStick();
private slots:
void onAxisChanged(int axis, double value);
private slots:
void showSettingDlgSlot();
@ -102,8 +92,5 @@ private:
QToolButton *hideCommandWidgetBtn = nullptr;
WebMapWidget *m_mapWidget = nullptr;
// JoystickController *m_joystick = nullptr;
DirectInputJoystick *m_dtjoystick;
};
#endif // MAINWINDOW_H

@ -4,8 +4,8 @@ PushStream::PushStream(QObject *parent) : QObject{parent} {
connect(this,&PushStream::initStreamPusherSignal,this,&PushStream::init,Qt::BlockingQueuedConnection);
}
void PushStream::setRemoteIP(QStringList& urls) {
m_pushStreamIPs = urls;
void PushStream::setRemoteIP(QString url) {
m_pushStreamIP = url;
}
bool PushStream::init(AVFormatContext *inputFormatCtx,
@ -14,18 +14,7 @@ bool PushStream::init(AVFormatContext *inputFormatCtx,
// m_pusherQueue = queue;
m_queueManager = queueManager;
m_inputFormatCtx = inputFormatCtx;
// m_start = openNetworkStream(inputFormatCtx);
bool ss = false;
for (int i = 0; i < m_pushStreamIPs.size(); ++i) {
AVFormatContext* outputformatCtx = nullptr;
ss = openNetworkStream(inputFormatCtx,outputformatCtx,m_pushStreamIPs.at(i));
m_outputFormatCtxArray.append(outputformatCtx);
m_initStatus.append(ss);
if(ss){
m_start = ss;
}
}
m_start = openNetworkStream(inputFormatCtx);
initStatus = m_start;
return m_start;
}
@ -37,37 +26,36 @@ void PushStream::close() {
// qDebug() << "*******m_end0:" << m_end;
}
bool PushStream::openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatContext *&outputFormatCtx,QString pushStreamIP) {
if (pushStreamIP.isEmpty()) return false;
bool PushStream::openNetworkStream(AVFormatContext *inputFormatCtx) {
if (m_pushStreamIP.isEmpty()) return false;
if (!inputFormatCtx) return false;
// 初始化网络输出流
// QString m_pushStreamIP = "rtsp://182.92.130.23/app/stream999";
QString format_name = "flv";
if (pushStreamIP.left(3) == "udp" || pushStreamIP.left(3) == "UDP")
if (m_pushStreamIP.left(3) == "udp" || m_pushStreamIP.left(3) == "UDP")
format_name = "mpegts";
int ret = avformat_alloc_output_context2(
&outputFormatCtx, NULL, format_name.toStdString().data(),
pushStreamIP.toUtf8().constData());
&m_outputFormatCtx, NULL, format_name.toStdString().data(),
m_pushStreamIP.toUtf8().constData());
if (ret < 0) {
showError(ret);
free(outputFormatCtx);
free();
qDebug() << "Could not create output context.";
return false;
}
AVStream *m_ostream = nullptr;
// 复制流信息
for (unsigned int i = 0; i < inputFormatCtx->nb_streams; ++i) {
// AVStream *stream = inputFormatCtx->streams[i];
if (inputFormatCtx->streams[i]->codecpar->codec_type ==
AVMEDIA_TYPE_VIDEO) {
m_istream = inputFormatCtx->streams[i];
m_ostream = avformat_new_stream(outputFormatCtx, nullptr);
m_ostream = avformat_new_stream(m_outputFormatCtx, nullptr);
if (!m_ostream) {
qDebug() << "Failed allocating output stream.\n";
free(outputFormatCtx);
free();
return false;
}
// 复制编解码器参数
@ -75,7 +63,7 @@ bool PushStream::openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatCont
m_istream->codecpar);
if (ret < 0) {
showError(ret);
free(outputFormatCtx);
free();
qWarning() << "avcodec_parameters_from_context Failed";
return false;
}
@ -90,22 +78,22 @@ bool PushStream::openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatCont
// 打开输出文件
if (!(outputFormatCtx->flags & AVFMT_NOFILE)) {
if (ret = avio_open(&outputFormatCtx->pb,
pushStreamIP.toUtf8().constData(),
if (!(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
if (ret = avio_open(&m_outputFormatCtx->pb,
m_pushStreamIP.toUtf8().constData(),
AVIO_FLAG_WRITE) < 0) {
showError(ret);
free(outputFormatCtx);
free();
qDebug() << "Could not open output file.\n";
return false;
}
}
// 写入头文件
ret = avformat_write_header(outputFormatCtx, NULL);
ret = avformat_write_header(m_outputFormatCtx, NULL);
if (ret < 0) {
showError(ret);
free(outputFormatCtx);
free();
qDebug() << "Error occurred when write_header into output file.\n";
return false;
}
@ -120,63 +108,57 @@ bool PushStream::openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatCont
return true;
}
int PushStream::reconnect(int ret,int id) {
int PushStream::reconnect(int ret) {
if(!m_queueManager->m_isPullReconnect) return 0;
if (ret == -10053 || ret == -10054) {
m_queueManager->m_isPushReconnect = true;
m_end = false;
// qDebug() << "m_end:" << m_end;
AVFormatContext* outputFormatCtx = m_outputFormatCtxArray.at(id);
QString message;
for (int nRetryCount = 0; nRetryCount < MAXCONNECT; ++nRetryCount) {
if(m_queueManager->m_isPullReconnect) {
break;
}
// 关闭输出
if (outputFormatCtx &&
!(outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(outputFormatCtx->pb);
if (m_outputFormatCtx &&
!(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(m_outputFormatCtx->pb);
}
ret =
avio_open(&outputFormatCtx->pb,
m_pushStreamIPs[id].toUtf8().constData(), AVIO_FLAG_WRITE);
avio_open(&m_outputFormatCtx->pb,
m_pushStreamIP.toUtf8().constData(), AVIO_FLAG_WRITE);
if (ret < 0) {
showError(ret);
qDebug() << "Failed to reconnect"
<< QString::number(nRetryCount + 1);
message =
QString("推流地址%1网络中断尝试重连第%2次!").arg(id+1,nRetryCount + 1);
emit sendErrorMessageSignal(message, 3);
QString str =
QString("网络中断,尝试重连第%1次!").arg(nRetryCount + 1);
emit sendErrorMessageSignal(str, 3);
if (m_end) break;
// av_usleep(5 * 1000000);
continue;
}
// Try to reconnect
ret = avformat_write_header(outputFormatCtx, nullptr);
ret = avformat_write_header(m_outputFormatCtx, nullptr);
if (ret < 0) {
m_initStatus[id] = false;
showError(ret);
// free();
qDebug() << "Failed to reconnect"
<< QString::number(nRetryCount + 1);
message =
QString("推流地址%1网络中断尝试重连第%2次!").arg(id+1,nRetryCount + 1);
emit sendErrorMessageSignal(message, 3);
QString str =
QString("网络中断,尝试重连第%1次!").arg(nRetryCount + 1);
emit sendErrorMessageSignal(str, 3);
if (m_end) break;
// nRetryCount++;
// av_usleep(5 * 1000000);
} else {
m_initStatus[id] = true;
m_start = true;
m_firstPts = AV_NOPTS_VALUE;
m_frm_cnt = 0;
m_bwriteHeader = true;
message =
QString("推流地址%1重连成功!").arg(id+1);
emit sendErrorMessageSignal(message, 1);
emit sendErrorMessageSignal("重连成功!", 1);
m_queueManager->clearPushQueue();
qDebug() << message;
qDebug() << "重连成功!";
m_queueManager->m_isPushReconnect = false;
return ret;
@ -185,16 +167,8 @@ int PushStream::reconnect(int ret,int id) {
if (m_end) break;
}
m_start = false;
for (int i = 0; i < m_initStatus.size(); ++i) {
if(m_initStatus.at(i)){
m_start = true;
break;
}
}
m_bwriteHeader = false;
message =
QString("推流地址%1重连失败推流停止!").arg(id+1);
emit sendErrorMessageSignal(message, 2);
emit sendErrorMessageSignal("重连失败,推流停止!", 2);
m_queueManager->m_isPushReconnect = false;
return -1;
}
@ -280,49 +254,29 @@ void PushStream::pushStream(int64_t startTime) {
// 向推流服务器推送流数据
m_frm_cnt++;
int ret;
for (int i = 0; i < m_outputFormatCtxArray.size(); ++i) {
if(!m_initStatus.at(i)) continue;
AVFormatContext* outputFormatCtx = m_outputFormatCtxArray.at(i);
if(outputFormatCtx!=nullptr){
AVPacket* clonePacket = nullptr;
if(i!= m_outputFormatCtxArray.size()-1){
clonePacket = av_packet_clone(inputPacket);
}else{
clonePacket = inputPacket;
}
ret =
av_interleaved_write_frame(outputFormatCtx, clonePacket);
if (ret < 0) {
av_packet_unref(clonePacket);
showError(ret);
// if (ret == -10053) {
// qDebug() << "网络不稳定";
// }
if (reconnect(ret,i) < 0) {
// break;
};
continue;
}
// 数据包写入成功现在可以释放pkt
av_packet_unref(clonePacket);
av_packet_free(&clonePacket);
}
int ret =
av_interleaved_write_frame(m_outputFormatCtx, inputPacket);
if (ret < 0) {
av_packet_unref(inputPacket);
showError(ret);
// if (ret == -10053) {
// qDebug() << "网络不稳定";
// }
if (reconnect(ret) < 0) {
break;
};
continue;
}
// 数据包写入成功现在可以释放pkt
av_packet_unref(inputPacket);
av_packet_free(&inputPacket);
} else {
// QThread::usleep(1000);
av_usleep(1000);
}
}
if (m_bwriteHeader) {
for (auto& outputFormatCtx : m_outputFormatCtxArray) {
if(outputFormatCtx!=nullptr){
av_write_trailer(outputFormatCtx);
}
}
}
if (m_bwriteHeader) av_write_trailer(m_outputFormatCtx);
free();
m_queueManager->m_isStreamPusherEnd = true;
m_queueManager->wakeStreamPusher();
@ -332,27 +286,13 @@ void PushStream::pushStream(int64_t startTime) {
}
void PushStream::free() {
for (auto &outputFormatCtx : m_outputFormatCtxArray) {
// 关闭输出
if (outputFormatCtx && !(outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(outputFormatCtx->pb);
}
if (outputFormatCtx) {
avformat_free_context(outputFormatCtx);
outputFormatCtx = nullptr;
}
}
m_outputFormatCtxArray.clear();
}
void PushStream::free(AVFormatContext *&outputFormatCtx)
{
m_start = false;
// 关闭输出
if (outputFormatCtx && !(outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(outputFormatCtx->pb);
if (m_outputFormatCtx && !(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(m_outputFormatCtx->pb);
}
if (outputFormatCtx) {
avformat_free_context(outputFormatCtx);
outputFormatCtx = nullptr;
if (m_outputFormatCtx) {
avformat_free_context(m_outputFormatCtx);
m_outputFormatCtx = nullptr;
}
}

@ -17,7 +17,7 @@ public:
* @brief
* @param url
*/
void setRemoteIP(QStringList& urls);
void setRemoteIP(QString url);
void close();
public slots:
@ -29,24 +29,22 @@ signals:
void sendErrorMessageSignal(QString message, int type);
void initStreamPusherSignal(AVFormatContext *inputFormatCtx,AVPacketQueueManager *queueManager);
private:
bool openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatContext *&outputFormatCtx,QString pushStreamIP);
int reconnect(int ret,int id=0);
bool openNetworkStream(AVFormatContext *inputFormatCtx);
int reconnect(int ret);
void free();
void free(AVFormatContext *&outputFormatCtx);
public:
bool initStatus = false;
private:
// QQueue<AVPacket *> *m_pusherQueue = nullptr;
AVFormatContext *m_inputFormatCtx = nullptr; //
AVFormatContext *m_outputFormatCtx = nullptr; //
AVFormatContext *m_outputFormatCtx2 = nullptr; //推流2
QVector<AVFormatContext *>m_outputFormatCtxArray;
AVFormatContext *m_outputFormatCtx = NULL; //
AVStream *m_istream = nullptr;
// AVStream *m_ostream = nullptr;
AVStream *m_ostream = nullptr;
bool m_bwriteHeader = false;
int m_videoIndex = -1;
QStringList m_pushStreamIPs; // 推流地址
QString m_pushStreamIP; // 推流地址
std::atomic<bool> m_start = false;
std::atomic<bool> m_end = false;
int64_t m_startTime;
@ -60,8 +58,6 @@ private:
AVRational m_inputTimeBase;
AVRational m_inputFrameRate;
AVRational m_outputTimeBase;
QVector<bool> m_initStatus;
};
#endif // PUSHSTREAM_H

@ -209,7 +209,6 @@ bool ReadStream::initSocket(QString ip, int port) {
}
}
}
}
bool ReadStream::initSavedRawFile(QString fileDir, QString uavName) {

@ -12,6 +12,7 @@
#include "avpacketqueuemanager.h"
#include "decodestream.h"
#include "ffmpeginclude.h"
#include "pushstream.h"
#include "savestream.h"

@ -171,8 +171,8 @@ void VideoWidget::stopPlay() {
}
// 推流
bool VideoWidget::pushStream(QStringList &urls) {
if (urls.isEmpty()) {
bool VideoWidget::pushStream(const QString &url) {
if (url.isEmpty()) {
return false;
} else {
// 先拉流
@ -192,7 +192,7 @@ bool VideoWidget::pushStream(QStringList &urls) {
connect(streamPusher, &PushStream::sendErrorMessageSignal, this,
&VideoWidget::receiveErrorMessage, Qt::UniqueConnection);
}
streamPusher->setRemoteIP(urls);
streamPusher->setRemoteIP(url);
streamPusher->moveToThread(&pushStreamThread);
pushStreamThread.start();
@ -203,7 +203,7 @@ bool VideoWidget::pushStream(QStringList &urls) {
return false;
}
m_pushURLs = urls;
m_pushURL = url;
m_pushFlag = true;
return true;
}
@ -230,8 +230,8 @@ void VideoWidget::setPullURL(const QString &url) {
m_pullURL = url;
}
void VideoWidget::setPushURL(QStringList &urls) {
m_pushURLs = urls;
void VideoWidget::setPushURL(const QString &url) {
m_pushURL = url;
}
void VideoWidget::setVedioSaveFileDirPath(const QString &dirPath) {

@ -46,10 +46,10 @@ public:
bool play(const QString &url, bool bSave = true);
bool udpPlay(QString ip, int port);
void stopPlay();
bool pushStream(QStringList &urls);
bool pushStream(const QString &url);
void stopPushStream();
void setPullURL(const QString &url);
void setPushURL(QStringList &urls);
void setPushURL(const QString &url);
void setVedioSaveFileDirPath(const QString &dirPath);
bool getPlayStatus();
double getVideoDuration();
@ -98,7 +98,7 @@ private:
private:
QString m_pullURL;
QStringList m_pushURLs;
QString m_pushURL;
bool m_playFlag = false;
bool m_pullFlag = false;
bool m_pushFlag = false;

Loading…
Cancel
Save