qt_rtmp_demo/mainwindow.cpp

262 lines
8.2 KiB
C++
Raw Normal View History

2023-11-12 16:13:24 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QPaintDevice>
2024-06-16 16:14:06 +00:00
2023-11-12 16:13:24 +00:00
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
MainWindow::MainWindow(QWidget *parent) :
QssMainWindow(parent,0,1.5),
ui(new Ui::MainWindow),
m_bCameraOpen(false),
mCamera(nullptr),
m_bRtmpPushing(false),
mPlayerWidget(nullptr),
mVideoCoder(nullptr),
mPusher(nullptr),
mAudioCapture(nullptr),
2023-12-17 16:33:54 +00:00
mTimer(nullptr),
mChart(nullptr),
mChartView(nullptr),
2024-06-16 16:14:06 +00:00
mSeries(nullptr),
mScreenCap(nullptr),
mCapScreen(false),
mScreenshot(nullptr)
2023-11-12 16:13:24 +00:00
{
ui->setupUi(this);
this->move(50,50);
int i(0);
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect clientRect = desktopWidget->availableGeometry();
QRect applicationRect = desktopWidget->screenGeometry();
qDebug()<<this->pos()<<clientRect<<applicationRect<<this->m_frame->geometry()<<this->centralWidget()->geometry();
std::vector<std::wstring> cameras = Camera::EnumAllCamera();
for(std::wstring x : cameras){
ui->comboBox->addItem(QString::fromWCharArray(x.c_str(),x.size()),
QString::fromWCharArray(x.c_str(),x.size()));
}
mAudioCapture = new CaptureAudioFfmpeg(44100, 2);
mMic = mAudioCapture->EnumSpeakers();
qDebug()<<"capture "<<mMic.size()<<"mic";
for(vector<CaptureAudioFfmpeg::MICInfo>::iterator itr = mMic.begin();itr != mMic.end();itr++){
qDebug()<<QString::fromStdWString(itr->name)<<itr->index;
2023-12-10 17:02:28 +00:00
ui->comboBox_2->addItem(QString::fromWCharArray(itr->name.c_str(),itr->name.size()),
QString::fromWCharArray(itr->name.c_str(),itr->name.size()));
2023-11-12 16:13:24 +00:00
}
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(DetectDpi()));
2023-12-17 16:33:54 +00:00
mTimer->start(500);
mChart = new QChart();
mSeries = new QLineSeries(this);
mSeries->setUseOpenGL(true);
mSeries->append(0, 0);
mChart->addSeries(mSeries);
mChart->createDefaultAxes();
mChart->axisX()->setMax(2000);
mChart->axisY()->setMax(2000);
mChart->axisY()->setMin(-2000);
mChartView = new QChartView(this);
ui->verticalLayout->addWidget(mChartView);
mChartView->setChart(mChart);
2024-06-16 16:14:06 +00:00
ui->verticalLayout->setStretch(0,2);
2023-12-17 16:33:54 +00:00
ui->verticalLayout->setStretch(1,8);
ui->verticalLayout->setStretch(2,4);
2023-11-12 16:13:24 +00:00
}
MainWindow::~MainWindow(){
delete ui;
}
2023-12-17 16:33:54 +00:00
void MainWindow::OnAudioData(const void *frameaddress, uint32_t framelen)
{
if(mBufferAudio.size() > 1) {
delete frameaddress;
return;
}
qDebug()<<frameaddress<<framelen;
mMux.lock();
mBufferAudio.push_front((void *)frameaddress);
mMux.unlock();
}
2023-11-12 16:13:24 +00:00
void MainWindow::on_pushButton_clicked(){
if(nullptr == mPlayerWidget){
mPlayerWidget = new CPlayWidget(nullptr);
}
if(!m_bCameraOpen){
mPlayerWidget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
qDebug()<<ui->comboBox->currentText().size()<<ui->comboBox->currentText();
wchar_t *opencamera = new wchar_t[ui->comboBox->currentText().size()];
ui->comboBox->currentText().toWCharArray(opencamera);
wstring ss = wstring(opencamera,ui->comboBox->currentText().size());
if(nullptr == mCamera){
this->mCamera = new Camera(ss);
}
2023-12-10 17:02:28 +00:00
GUIDToAvFormat(mCamera->MediaType());
mPlayerWidget->SetImgSize(mCamera->GetWidth(),mCamera->GetHeight());
2023-11-12 16:13:24 +00:00
this->mCamera->SetObserver(mPlayerWidget);
qDebug()<<ui->comboBox->currentText();
ui->pushButton->setText("关闭摄像头");
m_bCameraOpen = true;
mPlayerWidget->show();
2023-12-17 16:33:54 +00:00
ui->verticalLayout->insertWidget(2,mPlayerWidget,9);
2023-11-12 16:13:24 +00:00
qDebug()<<ui->verticalLayout->layout();
ui->verticalLayout->setStretch(0,1);
ui->verticalLayout->setStretch(1,0);
ui->verticalLayout->setStretch(2,9);
2023-12-17 16:33:54 +00:00
ui->verticalLayout->setStretch(3,5);
2023-11-12 16:13:24 +00:00
} else{
m_bCameraOpen = false;
ui->pushButton->setText("打开摄像头");
}
}
void MainWindow::on_pushButton_2_clicked()
{
if(!m_bRtmpPushing){
if(!m_bCameraOpen){
ToastWidget::showTip("请打开摄像头",this);
return;
}else{
//
if(nullptr == mVideoCoder){
mVideoCoder = new VideoCoder(mCamera->GetWidth(),
mCamera->GetHeight(),
GUIDToAvFormat(mCamera->MediaType()));
}
mCamera->SetObserver(mVideoCoder);
2023-11-29 16:22:43 +00:00
if(mPusher == nullptr)
mPusher = new H264RtmpPuser();
2023-11-12 16:13:24 +00:00
// todo 根据返回结果判断是否推流
qDebug()<<"连接RTMP服务器"<<ui->lineEdit->text();
if (!mPusher->IfConnect()) {
const char* address = ui->lineEdit->text().toLocal8Bit().data();
qDebug()<<address;
2023-12-10 17:02:28 +00:00
if (0 == mPusher->RTMP264_Connect("rtmp://127.0.0.1:1935/live/1")) {
2023-11-12 16:13:24 +00:00
ToastWidget::showTip("已经连接上RTMP服务器",this->parentWidget());
mVideoCoder->SetOberver(mPusher);
mPusher->StartPush();
ui->pushButton_2->setText("关闭推流");
/*
if (nullptr != this->mAudioCoder) {
this->mAudioCoder->SetObserver(mPusher);
//音频流先不推流
}*/
}
else {
ToastWidget::showTip("连接RTMP服务器失败请检查服务器地址",this->parentWidget());
}
}else{
ToastWidget::showTip("正在推流,请先关闭",this->parentWidget());
}
}
}
}
void MainWindow::on_pushButton_3_clicked()
{
qDebug()<<ui->comboBox_2->currentText();
2023-12-10 17:02:28 +00:00
2023-12-17 16:33:54 +00:00
mAudioCapture->InitCapture(ui->comboBox_2->currentText().toStdWString().c_str(),4800,2);
2023-12-10 17:02:28 +00:00
2023-12-12 15:01:12 +00:00
mAudioCapture->StartCapture();
2023-12-17 16:33:54 +00:00
mAudioCapture->SetObserver(this);
mSeries->removePoints(0,mSeries->points().size());
2023-12-12 15:01:12 +00:00
2023-11-12 16:13:24 +00:00
}
void MainWindow::DetectDpi()
{
int horizontalDPI = logicalDpiX();
int verticalDPI = logicalDpiY();
2023-12-17 16:33:54 +00:00
mMux.lock();
if(mBufferAudio.size() > 0){
uint8_t *ptr = static_cast<uint8_t *>(mBufferAudio.takeLast());
if(mSeries->points().size() > 100){
for(int i = 0;i < 2000 ;i++){
int16_t *v = (int16_t*)(ptr + i*40);
mSeries->replace(mSeries->at(i),QPointF(i,*v));
}
}else{
for(int i = 0;i < 2000 ;i++){
int16_t *v = (int16_t*)(ptr + i*40);
mSeries->append(QPointF(i,*v));
}
}
}
mMux.unlock();
2023-11-12 16:13:24 +00:00
}
2024-06-16 16:14:06 +00:00
void MainWindow::on_pushButton_4_clicked()
{
if(!mCapScreen){
if(nullptr == mScreenCap){
mScreenCap = new ScreenCapture();
}
mScreenCap->InitCap();
mScreenCap->InitFilter("crop=w=400:h=500:x=100:y=500");
mScreenCap->Start(this);
if(nullptr == mPlayerWidget){
mPlayerWidget = new CPlayWidget(nullptr);
}
mPlayerWidget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
mPlayerWidget->SetImgSize(300,300);
mScreenCap->SetObserver(this->mPlayerWidget);
mPlayerWidget->show();
ui->verticalLayout->insertWidget(2,mPlayerWidget,9);
qDebug()<<ui->verticalLayout->layout();
ui->verticalLayout->setStretch(0,1);
ui->verticalLayout->setStretch(1,0);
ui->verticalLayout->setStretch(2,9);
ui->verticalLayout->setStretch(3,5);
mCapScreen = true;
}
else{
mScreenCap->Stop();
}
}
void MainWindow::on_pushButton_5_clicked()
{
if(nullptr == mScreenshot){
mScreenshot = new ScreenShotWidget(nullptr);
connect(mScreenshot,&ScreenShotWidget::rect_selected,
this,[=](QRect rect){
qDebug()<<"screen rect selected"<<rect;
});
}
mScreenshot->showFullScreen();
}
void MainWindow::on_rect_selected(QRect rect)
{
qDebug()<<"screen rect selected"<<rect;
}