Compare commits

...

4 Commits

@ -13,9 +13,14 @@ 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"
@ -39,7 +44,11 @@ target_link_libraries(VideoClient
Qt6::Network
Qt6::SerialPort
Qt6::WebEngineWidgets
#Qt6::Gamepad
video
winmm
dinput8
dxguid
# QWindowKit::Widgets
# WidgetFrame
)

@ -145,3 +145,69 @@ 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,6 +122,12 @@ 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;

@ -0,0 +1,164 @@
#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;
}
}

@ -0,0 +1,47 @@
#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

@ -0,0 +1,106 @@
#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);
}
}

@ -0,0 +1,41 @@
#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,6 +32,7 @@ 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");
@ -60,6 +61,8 @@ MainWindow::MainWindow(QWidget *parent)
initHideButton();
//initJoyStick();
initDtJoyStick();
setSavedVideoDir();
// ui->stackedWidget->setCurrentIndex(1);
// ui->videoWidget1->play(list.at(0));
@ -123,6 +126,8 @@ void MainWindow::initSignalConnection() {
connect(ui->videoControlWidget, &VideoControl::stopVideoSignal, this,
&MainWindow::closeVideoSlot);
}
void MainWindow::initNotifyMessageConnection() {
@ -141,6 +146,38 @@ 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);
@ -468,7 +505,10 @@ void MainWindow::pushStreamSlot(bool bPush) {
} else {
pushStreamIP = g_pushStreamInfoStruct.pushStreamIP;
}
ui->videoWidget->pushStream(pushStreamIP);
QStringList pushStreamIPs;
pushStreamIPs.append(pushStreamIP);
// pushStreamIPs.append("rtmp://182.92.130.23/videoclient/fp98C_1_gcs");
ui->videoWidget->pushStream(pushStreamIPs);
} else {
ui->videoWidget->stopPushStream();
// ui->videoWidget1->stopPushStream();

@ -16,7 +16,9 @@
#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"
@ -68,6 +70,14 @@ private:
void setSavedVideoDir();
void initHideButton();
// void initJoyStick();
void initDtJoyStick();
private slots:
void onAxisChanged(int axis, double value);
private slots:
void showSettingDlgSlot();
@ -92,5 +102,8 @@ private:
QToolButton *hideCommandWidgetBtn = nullptr;
WebMapWidget *m_mapWidget = nullptr;
// JoystickController *m_joystick = nullptr;
DirectInputJoystick *m_dtjoystick;
};
#endif // MAINWINDOW_H

@ -95,3 +95,63 @@ bool AVPacketQueueManager::isEmptySeekQueue() {
QMutexLocker locker(&m_seekMutex);
return m_seekQueue.isEmpty();
}
void AVPacketQueueManager::waitForStreamDecoderClosed()
{
if(!m_isDecodeEnd){
m_waitCloseStreamDecoderMutex.lock();
qDebug()<<"waitForStreamDecoderClosed0";
m_condition.wait(&m_waitCloseStreamDecoderMutex);
m_waitCloseStreamDecoderMutex.unlock();
qDebug()<<"waitForStreamDecoderClosed1";
}
}
void AVPacketQueueManager::waitForStreamSaverClosed()
{
if(!m_isStreamSaverEnd){
m_waitCloseStreamSaverMutex.lock();
qDebug()<<"waitForStreamSaverClosed0";
m_condition.wait(&m_waitCloseStreamSaverMutex);
m_waitCloseStreamSaverMutex.unlock();
qDebug()<<"waitForStreamSaverClosed1";
}
}
void AVPacketQueueManager::waitForStreamPusherClosed()
{
if(!m_isStreamPusherEnd){
m_waitCloseStreamPusherMutex.lock();
qDebug()<<"waitForStreamPusherClosed0";
m_condition.wait(&m_waitCloseStreamPusherMutex);
m_waitCloseStreamPusherMutex.unlock();
qDebug()<<"waitForStreamPusherClosed1";
}
}
void AVPacketQueueManager::wakeStreamDecoder()
{
m_waitCloseStreamDecoderMutex.lock();
qDebug()<<"wakeStreamDecoder0";
m_condition.wakeAll();
m_waitCloseStreamDecoderMutex.unlock();
qDebug()<<"wakeStreamDecoder1";
}
void AVPacketQueueManager::wakeStreamSaver()
{
m_waitCloseStreamSaverMutex.lock();
qDebug()<<"wakeStreamSaver0";
m_condition.wakeAll();
m_waitCloseStreamSaverMutex.unlock();
qDebug()<<"wakeStreamSaver1";
}
void AVPacketQueueManager::wakeStreamPusher()
{
m_waitCloseStreamPusherMutex.lock();
m_condition.wakeAll();
qDebug()<<"wakeStreamPusher0";
m_waitCloseStreamPusherMutex.unlock();
qDebug()<<"wakeStreamPusher1";
}

@ -3,6 +3,7 @@
#include <QMutex>
#include <QQueue>
#include <QWaitCondition>
#include "ffmpeginclude.h"
@ -29,11 +30,22 @@ public:
bool isEmptySaveQueue();
bool isEmptySeekQueue();
void waitForStreamDecoderClosed();
void waitForStreamSaverClosed();
void waitForStreamPusherClosed();
void wakeStreamDecoder();
void wakeStreamSaver();
void wakeStreamPusher();
public:
std::atomic<bool> m_isSeeking{false}; // 是否正在跳转
std::atomic<bool> m_isReadEnd{false};
std::atomic<bool> m_isDecodeEnd{false};
std::atomic<bool> m_isStreamSaverEnd{false};
std::atomic<bool> m_isStreamPusherEnd{false};
std::atomic<bool> m_isPullReconnect{false};
std::atomic<bool> m_isPushReconnect{false};
private:
int QUEUECAPACITY = 100;
QQueue<AVPacket*> m_decodeQueue;
@ -44,6 +56,11 @@ private:
QMutex m_saveMutex; // 共享的互斥锁
QMutex m_pushMutex; // 共享的互斥锁
QMutex m_seekMutex; // 共享的互斥锁
private:
QWaitCondition m_condition;
QMutex m_waitCloseStreamDecoderMutex;
QMutex m_waitCloseStreamSaverMutex;
QMutex m_waitCloseStreamPusherMutex;
};
#endif // AVPACKETQUEUEMANAGER_H

@ -1,6 +1,9 @@
#include "decodestream.h"
DecodeStream::DecodeStream(QObject *parent) : QObject{parent}, m_playSpeed{1} {}
DecodeStream::DecodeStream(QObject *parent) : QObject{parent}, m_playSpeed{1} {
connect(this,&DecodeStream::initStreamDecoderSignal,this,&DecodeStream::init,Qt::BlockingQueuedConnection);
connect(this,&DecodeStream::initUDPStreamDecoderSignal,this,&DecodeStream::initUDPDecodeStream,Qt::BlockingQueuedConnection);
}
bool DecodeStream::init(AVPacketQueueManager *queueManager,
AVFormatContext *formatContext, int videoIndex) {
@ -22,13 +25,14 @@ bool DecodeStream::init(AVPacketQueueManager *queueManager,
if (m_fps <= 0) {
m_fps = 30.0; // 默认帧率
}
return initDecoder(formatContext, videoIndex);
initStatus = initDecoder(formatContext, videoIndex);
return initStatus;
}
bool DecodeStream::init(AVPacketQueueManager *queueManager) {
bool DecodeStream::initUDPDecodeStream(AVPacketQueueManager *queueManager) {
m_queueManager = queueManager;
return initUDPDecoder();
initStatus = initUDPDecoder();
return initStatus;
}
// 视频解码线程任务
@ -142,9 +146,12 @@ void DecodeStream::startDecode() {
}
free();
m_queueManager->m_isDecodeEnd = true;
m_queueManager->wakeStreamDecoder();
qDebug() << "Decoding Thread End!";
emit decodeEndSignal();
emit sendErrorMessageSignal("视频解码结束!", 1);
}
void DecodeStream::changePlaySpeedSlot(double speed) {

@ -18,7 +18,7 @@ public:
DecodeStream(QObject *parent = nullptr);
bool init(AVPacketQueueManager *queueManager,
AVFormatContext *formatContext, int videoIndex);
bool init(AVPacketQueueManager *queueManager);
bool initUDPDecodeStream(AVPacketQueueManager *queueManager);
void close();
AVCodecContext *getCodecContext();
AVCodecParserContext *getParserContext();
@ -31,7 +31,11 @@ signals:
void sendErrorMessageSignal(QString message, int type);
void updateVideoCurrentTime(int currentTime, int duration);
void decodeEndSignal();
void initUDPStreamDecoderSignal(AVPacketQueueManager *queueManager);
void initStreamDecoderSignal(AVPacketQueueManager *queueManager,
AVFormatContext *formatContext, int videoIndex);
public:
bool initStatus = false;
private:
bool initObject(); // 初始化对象
bool initDecoder(AVFormatContext *inputFormatContext,
@ -44,7 +48,7 @@ private:
bool isValidAVPacket(AVPacket *pkt);
private:
bool m_start = true;
std::atomic<bool>m_start = true;
int m_videoIndex = 0;
AVFormatContext *m_formatContext = nullptr;
AVCodecContext *m_codecContext = nullptr; // 解码器上下文

@ -22,7 +22,6 @@ extern "C" {
#include <QElapsedTimer>
#include <QEventLoop>
// #include "global.h"
#define PRINT_LOG 1
#define ERROR_LEN 1024 // 异常信息数组长度

@ -1,17 +1,32 @@
#include "pushstream.h"
PushStream::PushStream(QObject *parent) : QObject{parent} {}
PushStream::PushStream(QObject *parent) : QObject{parent} {
connect(this,&PushStream::initStreamPusherSignal,this,&PushStream::init,Qt::BlockingQueuedConnection);
}
void PushStream::setRemoteIP(QString url) {
m_pushStreamIP = url;
void PushStream::setRemoteIP(QStringList& urls) {
m_pushStreamIPs = urls;
}
bool PushStream::init(AVFormatContext *inputFormatCtx,
AVPacketQueueManager *queueManager) {
qDebug() << "PushStream::init ThreadID:" << QThread::currentThreadId();
// m_pusherQueue = queue;
m_queueManager = queueManager;
m_inputFormatCtx = inputFormatCtx;
m_start = openNetworkStream(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;
}
}
initStatus = m_start;
return m_start;
}
@ -22,36 +37,37 @@ void PushStream::close() {
// qDebug() << "*******m_end0:" << m_end;
}
bool PushStream::openNetworkStream(AVFormatContext *inputFormatCtx) {
if (m_pushStreamIP.isEmpty()) return false;
bool PushStream::openNetworkStream(AVFormatContext *&inputFormatCtx,AVFormatContext *&outputFormatCtx,QString pushStreamIP) {
if (pushStreamIP.isEmpty()) return false;
if (!inputFormatCtx) return false;
// 初始化网络输出流
// QString m_pushStreamIP = "rtsp://182.92.130.23/app/stream999";
QString format_name = "flv";
if (m_pushStreamIP.left(3) == "udp" || m_pushStreamIP.left(3) == "UDP")
if (pushStreamIP.left(3) == "udp" || pushStreamIP.left(3) == "UDP")
format_name = "mpegts";
int ret = avformat_alloc_output_context2(
&m_outputFormatCtx, NULL, format_name.toStdString().data(),
m_pushStreamIP.toUtf8().constData());
&outputFormatCtx, NULL, format_name.toStdString().data(),
pushStreamIP.toUtf8().constData());
if (ret < 0) {
showError(ret);
free();
free(outputFormatCtx);
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(m_outputFormatCtx, nullptr);
m_ostream = avformat_new_stream(outputFormatCtx, nullptr);
if (!m_ostream) {
qDebug() << "Failed allocating output stream.\n";
free();
free(outputFormatCtx);
return false;
}
// 复制编解码器参数
@ -59,7 +75,7 @@ bool PushStream::openNetworkStream(AVFormatContext *inputFormatCtx) {
m_istream->codecpar);
if (ret < 0) {
showError(ret);
free();
free(outputFormatCtx);
qWarning() << "avcodec_parameters_from_context Failed";
return false;
}
@ -71,25 +87,25 @@ bool PushStream::openNetworkStream(AVFormatContext *inputFormatCtx) {
m_inputTimeBase = m_istream->time_base;
m_inputFrameRate = m_istream->r_frame_rate;
m_outputTimeBase = m_istream->time_base;
// 打开输出文件
if (!(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
if (ret = avio_open(&m_outputFormatCtx->pb,
m_pushStreamIP.toUtf8().constData(),
if (!(outputFormatCtx->flags & AVFMT_NOFILE)) {
if (ret = avio_open(&outputFormatCtx->pb,
pushStreamIP.toUtf8().constData(),
AVIO_FLAG_WRITE) < 0) {
showError(ret);
free();
free(outputFormatCtx);
qDebug() << "Could not open output file.\n";
return false;
}
}
// 写入头文件
ret = avformat_write_header(m_outputFormatCtx, NULL);
ret = avformat_write_header(outputFormatCtx, NULL);
if (ret < 0) {
showError(ret);
free();
free(outputFormatCtx);
qDebug() << "Error occurred when write_header into output file.\n";
return false;
}
@ -97,64 +113,89 @@ bool PushStream::openNetworkStream(AVFormatContext *inputFormatCtx) {
// m_InitStatus = true;
// startTime = av_gettime_relative();
m_outputTimeBase = m_ostream->time_base;
m_bwriteHeader = true;
m_firstPts = AV_NOPTS_VALUE;
return true;
}
int PushStream::reconnect(int ret) {
int PushStream::reconnect(int ret,int id) {
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 (m_outputFormatCtx &&
!(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(m_outputFormatCtx->pb);
if (outputFormatCtx &&
!(outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(outputFormatCtx->pb);
}
ret =
avio_open(&m_outputFormatCtx->pb,
m_pushStreamIP.toUtf8().constData(), AVIO_FLAG_WRITE);
avio_open(&outputFormatCtx->pb,
m_pushStreamIPs[id].toUtf8().constData(), AVIO_FLAG_WRITE);
if (ret < 0) {
showError(ret);
qDebug() << "Failed to reconnect"
<< QString::number(nRetryCount + 1);
QString str =
QString("网络中断,尝试重连第%1次!").arg(nRetryCount + 1);
emit sendErrorMessageSignal(str, 3);
message =
QString("推流地址%1网络中断尝试重连第%2次!").arg(id+1,nRetryCount + 1);
emit sendErrorMessageSignal(message, 3);
if (m_end) break;
// av_usleep(5 * 1000000);
continue;
}
// Try to reconnect
ret = avformat_write_header(m_outputFormatCtx, nullptr);
ret = avformat_write_header(outputFormatCtx, nullptr);
if (ret < 0) {
m_initStatus[id] = false;
showError(ret);
// free();
qDebug() << "Failed to reconnect"
<< QString::number(nRetryCount + 1);
QString str =
QString("网络中断,尝试重连第%1次!").arg(nRetryCount + 1);
emit sendErrorMessageSignal(str, 3);
message =
QString("推流地址%1网络中断尝试重连第%2次!").arg(id+1,nRetryCount + 1);
emit sendErrorMessageSignal(message, 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;
emit sendErrorMessageSignal("重连成功!", 1);
message =
QString("推流地址%1重连成功!").arg(id+1);
emit sendErrorMessageSignal(message, 1);
m_queueManager->clearPushQueue();
qDebug() << "重连成功!";
qDebug() << message;
m_queueManager->m_isPushReconnect = false;
return ret;
}
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;
emit sendErrorMessageSignal("重连失败,推流停止!", 2);
message =
QString("推流地址%1重连失败推流停止!").arg(id+1);
emit sendErrorMessageSignal(message, 2);
m_queueManager->m_isPushReconnect = false;
return -1;
}
return 0;
@ -163,6 +204,7 @@ int PushStream::reconnect(int ret) {
void PushStream::pushStream(int64_t startTime) {
qDebug() << "PushStreamThreadID:" << QThread::currentThreadId();
// m_startTime = startTime;
m_queueManager->m_isStreamPusherEnd = false;
while (m_start) {
AVPacket *inputPacket = m_queueManager->dequeuePushPacket();
if (inputPacket) {
@ -213,7 +255,7 @@ void PushStream::pushStream(int64_t startTime) {
// sleepMsec(40);
} else {
if (delay < -100000) {
qDebug() << "delay:" << delay;
qDebug() << "push delay:" << delay;
// 滞后100ms以上丢弃非重要帧
if (!(inputPacket->flags & AV_PKT_FLAG_KEY)) {
av_packet_unref(inputPacket);
@ -238,42 +280,79 @@ void PushStream::pushStream(int64_t startTime) {
// 向推流服务器推送流数据
m_frm_cnt++;
int ret =
av_interleaved_write_frame(m_outputFormatCtx, inputPacket);
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(inputPacket);
av_packet_unref(clonePacket);
showError(ret);
// if (ret == -10053) {
// qDebug() << "网络不稳定";
// }
if (reconnect(ret) < 0) {
break;
if (reconnect(ret,i) < 0) {
// break;
};
continue;
}
// 数据包写入成功现在可以释放pkt
av_packet_unref(inputPacket);
av_packet_free(&inputPacket);
av_packet_unref(clonePacket);
av_packet_free(&clonePacket);
}
}
} else {
// QThread::usleep(1000);
av_usleep(1000);
}
}
if (m_bwriteHeader) av_write_trailer(m_outputFormatCtx);
if (m_bwriteHeader) {
for (auto& outputFormatCtx : m_outputFormatCtxArray) {
if(outputFormatCtx!=nullptr){
av_write_trailer(outputFormatCtx);
}
}
}
free();
m_queueManager->m_isStreamPusherEnd = true;
m_queueManager->wakeStreamPusher();
qDebug() << "Push Stream End!";
emit sendErrorMessageSignal("推流结束!", 1);
}
void PushStream::free() {
m_start = false;
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)
{
// 关闭输出
if (m_outputFormatCtx && !(m_outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(m_outputFormatCtx->pb);
if (outputFormatCtx && !(outputFormatCtx->flags & AVFMT_NOFILE)) {
avio_close(outputFormatCtx->pb);
}
if (m_outputFormatCtx) {
avformat_free_context(m_outputFormatCtx);
m_outputFormatCtx = nullptr;
if (outputFormatCtx) {
avformat_free_context(outputFormatCtx);
outputFormatCtx = nullptr;
}
}

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

@ -105,8 +105,10 @@ bool ReadStream::setStreamDecoder(DecodeStream *decodeStreamer) {
// QMutexLocker locker(&m_mutex);
m_streamDecoder = decodeStreamer;
m_queueManager.clearDecodeQueue();
m_decodeStreamFlag = m_streamDecoder->init(
&m_queueManager, m_formatContext, m_videoIndex);
// m_decodeStreamFlag = m_streamDecoder->init(
// &m_queueManager, m_formatContext, m_videoIndex);
m_streamDecoder->initStreamDecoderSignal(&m_queueManager, m_formatContext, m_videoIndex);
m_decodeStreamFlag = m_streamDecoder->initStatus;
if (m_decodeStreamFlag) emit decodeStreamer->startDecodeSignal();
return m_decodeStreamFlag;
} else {
@ -120,9 +122,11 @@ bool ReadStream::setUDPStreamDecoder(DecodeStream *decodeStreamer) {
// QMutexLocker locker(&m_mutex);
m_streamDecoder = decodeStreamer;
m_queueManager.clearDecodeQueue();
m_decodeStreamFlag = m_streamDecoder->init(&m_queueManager);
// m_decodeStreamFlag = m_streamDecoder->init(&m_queueManager);
// codec_ctx = m_streamDecoder->getCodecContext();
// parser = m_streamDecoder->getParserContext();
m_streamDecoder->initUDPStreamDecoderSignal(&m_queueManager);
m_decodeStreamFlag = m_streamDecoder->initStatus;
if (m_decodeStreamFlag) emit decodeStreamer->startDecodeSignal();
return m_decodeStreamFlag;
} else {
@ -138,8 +142,10 @@ bool ReadStream::setStreamSaver(SaveStream *streamSaver, bool isUDP) {
if (isUDP) {
m_saveStreamFlag = m_streamSaver->initUDP(&m_queueManager);
} else {
m_saveStreamFlag = m_streamSaver->init(
m_formatContext, &m_queueManager, m_videoIndex);
// m_saveStreamFlag = m_streamSaver->init(
// m_formatContext, &m_queueManager, m_videoIndex);
emit m_streamSaver->initStreamSaverSignal(m_formatContext, &m_queueManager, m_videoIndex);
m_saveStreamFlag = m_streamSaver->initStatus;
}
return m_saveStreamFlag;
} else {
@ -149,10 +155,13 @@ bool ReadStream::setStreamSaver(SaveStream *streamSaver, bool isUDP) {
}
bool ReadStream::setStreamPusher(PushStream *streamPusher) {
qDebug() << "setStreamPusher ThreadID:" << QThread::currentThreadId();
if (streamPusher) {
m_streamPusher = streamPusher;
m_queueManager.clearPushQueue();
m_pushStreamFlag = streamPusher->init(m_formatContext, &m_queueManager);
emit streamPusher->initStreamPusherSignal(m_formatContext, &m_queueManager);
// m_pushStreamFlag = streamPusher->init(m_formatContext, &m_queueManager);
m_pushStreamFlag = m_streamPusher->initStatus;
if (m_pushStreamFlag) streamPusher->startPushStreamSignal(0);
return m_pushStreamFlag;
} else {
@ -200,6 +209,7 @@ bool ReadStream::initSocket(QString ip, int port) {
}
}
}
}
bool ReadStream::initSavedRawFile(QString fileDir, QString uavName) {
@ -471,21 +481,25 @@ void ReadStream::free() {
}
bool ReadStream::reconnect() {
m_queueManager.m_isPullReconnect = true;
m_end = false;
if (m_streamDecoder) {
m_streamDecoder->close();
m_queueManager.waitForStreamDecoderClosed();
}
if (m_streamSaver) {
m_streamSaver->close();
m_queueManager.waitForStreamSaverClosed();
}
if (m_streamPusher) {
m_streamPusher->close();
m_queueManager.waitForStreamPusherClosed();
}
qDebug()<<"all closed!";
free();
for (int i = 0; i < MAXRECONNECT; ++i) {
m_start = openFile(m_pullURL);
if (m_start) {
emit sendErrorMessageSignal("重连成功!", 1);
if (m_streamDecoder) {
setStreamDecoder(m_streamDecoder);
}
@ -495,6 +509,8 @@ bool ReadStream::reconnect() {
if (m_streamPusher) {
setStreamPusher(m_streamPusher);
}
m_queueManager.m_isPullReconnect = false;
emit sendErrorMessageSignal("重连成功!", 1);
return true;
} else {
qDebug() << "reconnect failed:" << QString::number(i + 1);
@ -506,6 +522,7 @@ bool ReadStream::reconnect() {
if (m_end) break;
}
emit sendErrorMessageSignal("重连失败!", 2);
m_queueManager.m_isPullReconnect = false;
return false;
}

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

@ -1,6 +1,8 @@
#include "savestream.h"
SaveStream::SaveStream(QObject *parent) : QObject{parent} {}
SaveStream::SaveStream(QObject *parent) : QObject{parent} {
connect(this,&SaveStream::initStreamSaverSignal,this,&SaveStream::init,Qt::BlockingQueuedConnection);
}
bool SaveStream::init(AVFormatContext *formatContext,
AVPacketQueueManager *queueManager, int videoIndex) {
@ -13,6 +15,7 @@ bool SaveStream::init(AVFormatContext *formatContext,
if (!m_start) {
free();
}
initStatus = m_start;
return m_start;
}
@ -37,8 +40,10 @@ void SaveStream::close() {
void SaveStream::startSaveStream() {
qDebug() << "SaveStreamThreadID:" << QThread::currentThreadId();
if (!m_start) {
m_queueManager->m_isStreamSaverEnd = true;
return;
}
m_queueManager->m_isStreamSaverEnd = false;
int frameIndex = 0;
int64_t dts = 0;
int64_t dts_last = 0;
@ -101,16 +106,6 @@ void SaveStream::startSaveStream() {
}
}
// AVStream *out_stream = m_formatContextSave->streams[0];
// out_stream->start_time = 0;
// m_formatContextSave->start_time = 0;
// int64_t duration_pts = last_pts - first_pts;
// // duration_pts = av_rescale(duration_pts, 1, AV_TIME_BASE);
// m_formatContextSave->duration = duration_pts;
// AVStream *stream = m_formatContextSave->streams[0];
// stream->duration = duration_pts;
// 写入文件尾
if (m_formatContextSave && m_writeHeader) {
av_write_trailer(m_formatContextSave);
@ -123,6 +118,8 @@ void SaveStream::startSaveStream() {
}
free();
m_queueManager->m_isStreamSaverEnd = true;
m_queueManager->wakeStreamSaver();
qDebug() << "Save Video End";
emit sendErrorMessageSignal("视频保存结束!", 1);
}
@ -130,8 +127,11 @@ void SaveStream::startSaveStream() {
void SaveStream::startSaveUDPStream() {
qDebug() << "SaveStreamThreadID:" << QThread::currentThreadId();
if (!m_start) {
m_queueManager->m_isStreamSaverEnd = true;
qDebug()<<"StreamSaver direct return ";
return;
}
m_queueManager->m_isStreamSaverEnd = false;
int frameIndex = 0;
int64_t pts = 0;
int64_t dts = 0;
@ -184,8 +184,11 @@ void SaveStream::startSaveUDPStream() {
}
free();
m_queueManager->m_isStreamSaverEnd = true;
m_queueManager->wakeStreamSaver();
qDebug() << "视频保存结束!";
emit sendErrorMessageSignal("视频保存结束!", 1);
}
bool SaveStream::openFile(bool isUDP) {

@ -30,12 +30,16 @@ public slots:
signals:
void startSaveStreamSignal();
void sendErrorMessageSignal(QString message, int type);
void initStreamSaverSignal(AVFormatContext *formatContext,
AVPacketQueueManager *queueManager, int videoIndex);
private:
bool openFile(bool isUDP = false);
void free();
bool isValidAVPacket(AVPacket *pkt);
public:
bool initStatus = false;
private:
AVFormatContext *m_formatContextSave = nullptr; // 封装上下文
AVFormatContext *m_inputFormatContext = nullptr;

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

Loading…
Cancel
Save