qt_rtmp_demo/mainwindow.cpp

142 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QPaintDevice>
#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),
mTimer(nullptr)
{
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;
}
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(DetectDpi()));
mTimer->start(100);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::on_pushButton_clicked(){
if(nullptr == mPlayerWidget){
mPlayerWidget = new CPlayWidget(nullptr);
}
if(!m_bCameraOpen){
mPlayerWidget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
mPlayerWidget->SetImgSize(640,480);
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);
}
this->mCamera->SetObserver(mPlayerWidget);
qDebug()<<ui->comboBox->currentText();
ui->pushButton->setText("关闭摄像头");
m_bCameraOpen = true;
mPlayerWidget->show();
ui->verticalLayout->addWidget(mPlayerWidget);
qDebug()<<ui->verticalLayout->layout();
ui->verticalLayout->setStretch(0,1);
ui->verticalLayout->setStretch(1,0);
ui->verticalLayout->setStretch(2,9);
} 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);
if(mPusher == nullptr)
mPusher = new H264RtmpPuser();
// todo 根据返回结果判断是否推流
qDebug()<<"连接RTMP服务器"<<ui->lineEdit->text();
if (!mPusher->IfConnect()) {
const char* address = ui->lineEdit->text().toLocal8Bit().data();
qDebug()<<address;
if (0 == mPusher->RTMP264_Connect("rtmp://hyrtc.net:2530/live/123")) {
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();
}
void MainWindow::DetectDpi()
{
// qDebug()<<"detect dpi";
int horizontalDPI = logicalDpiX();
int verticalDPI = logicalDpiY();
// qDebug()<<horizontalDPI<<verticalDPI<<physicalDpiX()<<physicalDpiY();
}