C++ Qt实现浏览器网页内嵌的音视频播放器
#include
#include
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ctaudioplayer.h"
#include
#include
#include
#include
#include
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include
#else
#include
#endif
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始化多路播放器
InitMul();
//初始化websocket
InitWeb();
//窗口置顶
this->setWindowFlags(Qt::Widget | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
}
MainWindow::~MainWindow()
{
if(m_pWebSocketServer)
m_pWebSocketServer->close();
delete ui;
}
void MainWindow::InitMul()
{
qRegisterMetaType
connect(this, SIGNAL(sig_setScreenType(int)),
ui->widget_mulvideo, SLOT(slot_setScreenType(int)));
connect(this, SIGNAL(sig_playOne(MEDIA_DEV_INFO_T)),
ui->widget_mulvideo, SLOT(slot_playOne(MEDIA_DEV_INFO_T)));
connect(this, SIGNAL(sig_stopOne(int)),
ui->widget_mulvideo, SLOT(slot_stopOne(int)));
connect(this, SIGNAL(sig_snapshot(int)),
ui->widget_mulvideo, SLOT(slot_snapshot(int)));
connect(this, SIGNAL(sig_enableRecord(bool,int)),
ui->widget_mulvideo, SLOT(slot_enableRecord(bool,int)));
connect(this, SIGNAL(sig_stopAll()),
ui->widget_mulvideo, SLOT(slot_stopAll()));
connect(this, SIGNAL(sig_nextPage()),
ui->widget_mulvideo, SLOT(slot_NextPage()));
connect(this, SIGNAL(sig_prevPage()),
ui->widget_mulvideo, SLOT(slot_PrevPage()));
connect(ui->widget_mulvideo, SIGNAL(sig_pageInfo(QString)),
this, SLOT(slot_setPageInfo(QString)));
connect(ui->widget_mulvideo, SIGNAL(sig_curWinIndex(int)),
this, SLOT(slot_curWinIndex(int)));
connect(ui->widget_mulvideo, SIGNAL(sig_playFailTip(QString)),
this, SLOT(slot_playFailTip(QString)));
connect(this, SIGNAL(sig_fullscreen(bool)),
ui->widget_mulvideo, SLOT(slot_fullscreen(bool)));
connect(ui->widget_mulvideo, SIGNAL(sig_fullscreen(bool)),
this, SLOT(slot_fullscreen(bool)));
ui->comboBox_ChangeVideo->setCurrentIndex(1);
emit sig_setScreenType(1);
}
void MainWindow::InitWeb()
{
//web param
m_stWebParam.sInfo = SOFTWARE_VERSION;
m_stWebParam.sVer = SOFTWARE_VERSION;
m_stWebParam.nCode = 0;
m_stWebParam.nHwnd = 0;
//窗口置顶
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
//设置背景
QColor color("#f0faff");
QPalette pal(this->palette());
pal.setColor(QPalette::Background, color);
this->setAutoFillBackground(true);
this->setPalette(pal);
//websocket
m_pWebSocketServer = new QWebSocketServer("myServer", QWebSocketServer::NonSecureMode);
connect(m_pWebSocketServer, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
m_pWebSocketServer->listen(QHostAddress::Any, WEB_LISTEN_PORT);
InitMethodFun();
}
void MainWindow::InitMethodFun()
{
METHOD_FUN_T stMethodFun[] =
{
{"window.version", MainWindow::GetPlayerVer},
{"window.create", MainWindow::windowCreate},
{"window.change", MainWindow::windowChange},
{"window.show", MainWindow::windowShow},
{"media.playReal", MainWindow::PlayReal},
{"media.stop", MainWindow::StopMedia},
{"media.snapshot", MainWindow::Snapshot},
{"media.enableRecord", MainWindow::enableRecord},
{"media.enableAudio", MainWindow::enableAudio},
{"media.setAudioVolumn", MainWindow::setAudioVolumn},
{"media.fullscreen", MainWindow::fullScreen},
{"player.test", NULL},
};
for(int i=0; stMethodFun[i].methodFun != NULL; i++)
{
m_hashFun.insert(stMethodFun[i].sMethod, stMethodFun[i].methodFun);
}
}
void MainWindow::SendJsonData(QJsonObject Json)
{
//构建 Json 文档
QJsonDocument document;
document.setObject(Json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
for (int i=0;i { m_clientsList.at(i)->sendTextMessage(strJson); } } void MainWindow::GetPlayerVer(void* pObject, QJsonObject* pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); MY_DEBUG << "yibin test m_stWebParam.nID:" << pWin->m_stWebParam.nID; QJsonObject dataObject; dataObject.insert("info", pWin->m_stWebParam.sInfo); dataObject.insert("ver", pWin->m_stWebParam.sVer); QJsonObject json; json.insert("code", pWin->m_stWebParam.nCode); json.insert("data", QJsonValue(dataObject)); json.insert("id", pWin->m_stWebParam.nID); json.insert("session", pWin->m_stWebParam.nSession); json.insert("success", "true"); pWin->SendJsonData(json); } } } void MainWindow::windowCreate(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("clientAreaWidth")) { QJsonValue value = obj.value("clientAreaWidth"); if(value.isDouble()) { pWin->m_stWebParam.nClientAreaWidth = value.toVariant().toInt(); } } if(obj.contains("clientAreaHeight")) { QJsonValue value = obj.value("clientAreaHeight"); if(value.isDouble()) { pWin->m_stWebParam.nClientAreaHeight = value.toVariant().toInt(); } } if(obj.contains("width")) { QJsonValue value = obj.value("width"); if(value.isDouble()) { pWin->m_stWebParam.nWidth = value.toVariant().toInt(); } } if(obj.contains("height")) { QJsonValue value = obj.value("height"); if(value.isDouble()) { pWin->m_stWebParam.nHeight = value.toVariant().toInt(); } } if(obj.contains("left")) { QJsonValue value = obj.value("left"); if(value.isDouble()) { pWin->m_stWebParam.nLeft = value.toVariant().toInt(); } } if(obj.contains("top")) { QJsonValue value = obj.value("top"); if(value.isDouble()) { pWin->m_stWebParam.nTop = value.toVariant().toInt(); } } if(obj.contains("num")) { QJsonValue value = obj.value("num"); if(value.isDouble()) { pWin->m_stWebParam.nNum = value.toVariant().toInt(); } } } pWin->move(pWin->m_stWebParam.nScreenX+pWin->m_stWebParam.nLeft, pWin->m_stWebParam.nScreenY+pWin->m_stWebParam.nTop+140); } QJsonObject dataObject; dataObject.insert("bRtsps", pWin->m_stWebParam.bRtsp); dataObject.insert("hwnd", pWin->m_stWebParam.nHwnd); QJsonObject json; json.insert("code", pWin->m_stWebParam.nCode); json.insert("data", QJsonValue(dataObject)); json.insert("id", pWin->m_stWebParam.nID); json.insert("session", pWin->m_stWebParam.nSession); json.insert("success", "true"); pWin->SendJsonData(json); } void MainWindow::windowChange(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("clientAreaWidth")) { QJsonValue value = obj.value("clientAreaWidth"); if(value.isDouble()) { pWin->m_stWebParam.nClientAreaWidth = value.toVariant().toInt(); } } if(obj.contains("clientAreaHeight")) { QJsonValue value = obj.value("clientAreaHeight"); if(value.isDouble()) { pWin->m_stWebParam.nClientAreaHeight = value.toVariant().toInt(); } } if(obj.contains("left")) { QJsonValue value = obj.value("left"); if(value.isDouble()) { pWin->m_stWebParam.nLeft = value.toVariant().toInt(); } } if(obj.contains("top")) { QJsonValue value = obj.value("top"); if(value.isDouble()) { pWin->m_stWebParam.nTop = value.toVariant().toInt(); } } if(obj.contains("screenX")) { QJsonValue value = obj.value("screenX"); if(value.isDouble()) { pWin->m_stWebParam.nScreenX = value.toVariant().toInt(); } } if(obj.contains("screenY")) { QJsonValue value = obj.value("screenY"); if(value.isDouble()) { pWin->m_stWebParam.nScreenY = value.toVariant().toInt(); } } } } pWin->move(pWin->m_stWebParam.nScreenX+pWin->m_stWebParam.nLeft, pWin->m_stWebParam.nScreenY+pWin->m_stWebParam.nTop+140); } void MainWindow::windowShow(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("show")) { QJsonValue value = obj.value("show"); if(value.isBool()) { pWin->m_stWebParam.bShow = value.toVariant().toBool(); } } if(obj.contains("hwnd")) { QJsonValue value = obj.value("hwnd"); if(value.isDouble()) { pWin->m_stWebParam.nHwnd = value.toVariant().toInt(); } } if(obj.contains("browserType")) { QJsonValue value = obj.value("browserType"); if(value.isDouble()) { pWin->m_stWebParam.nBrowserType = value.toVariant().toInt(); } } } } pWin->setVisible(pWin->m_stWebParam.bShow); } void MainWindow::PlayReal(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("devId")) { QJsonValue value = obj.value("devId"); if(value.isString()) { pWin->m_stWebParam.sDevId = value.toVariant().toString(); } } if(obj.contains("winIndex")) { QJsonValue value = obj.value("winIndex"); if(value.isDouble()) { pWin->m_stWebParam.nWinIndex = value.toVariant().toInt(); } } if(obj.contains("decodeType")) { QJsonValue value = obj.value("decodeType"); if(value.isDouble()) { pWin->m_stWebParam.nDecodeType = value.toVariant().toInt(); } } if(obj.contains("connectType")) { QJsonValue value = obj.value("connectType"); if(value.isDouble()) { pWin->m_stWebParam.nProtocolType = value.toVariant().toInt(); } } if(obj.contains("url")) { QJsonValue value = obj.value("url"); if(value.isString()) { pWin->m_stWebParam.sUrl = value.toVariant().toString(); } } } } if(!pWin->m_stWebParam.sUrl.isEmpty() && !pWin->m_stWebParam.sDevId.isEmpty() && (pWin->m_stWebParam.nWinIndex >= 0 && pWin->m_stWebParam.nWinIndex < MAX_MEDIA_NUM)) { MEDIA_DEV_INFO_T stDev; stDev.nChannel = pWin->m_stWebParam.nWinIndex; stDev.sDevId = pWin->m_stWebParam.sDevId; stDev.sUrl = pWin->m_stWebParam.sUrl; stDev.nDecodeType = pWin->m_stWebParam.nDecodeType; stDev.nProtocolType = pWin->m_stWebParam.nProtocolType; emit pWin->sig_playOne(stDev); } QJsonObject dataObject; QJsonObject json; json.insert("code", pWin->m_stWebParam.nCode); json.insert("data", QJsonValue(dataObject)); json.insert("id", pWin->m_stWebParam.nID); json.insert("session", pWin->m_stWebParam.nSession); json.insert("success", "true"); pWin->SendJsonData(json); } void MainWindow::Snapshot(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("winIndex")) { QJsonValue value = obj.value("winIndex"); if(value.isDouble()) { pWin->m_stWebParam.nWinIndex = value.toVariant().toInt(); } } } } emit pWin->sig_snapshot(pWin->m_stWebParam.nWinIndex); } void MainWindow::enableRecord(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("winIndex")) { QJsonValue value = obj.value("winIndex"); if(value.isDouble()) { pWin->m_stWebParam.nWinIndex = value.toVariant().toInt(); } } if(obj.contains("isEnable")) { QJsonValue value = obj.value("isEnable"); if(value.isBool()) { pWin->m_stWebParam.bRecord = value.toVariant().toBool(); } } } } emit pWin->sig_enableRecord(pWin->m_stWebParam.bRecord, pWin->m_stWebParam.nWinIndex); } void MainWindow::enableAudio(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("isEnable")) { QJsonValue value = obj.value("isEnable"); if(value.isBool()) { pWin->m_stWebParam.bAudio = value.toVariant().toBool(); ctAudioPlayer::getInstance().isPlay(pWin->m_stWebParam.bAudio); } } } } } void MainWindow::setAudioVolumn(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("volumn")) { QJsonValue value = obj.value("volumn"); if(value.isDouble()) { pWin->m_stWebParam.nVolumn = value.toVariant().toDouble(); qreal nVal = pWin->m_stWebParam.nVolumn / 100.0; MY_DEBUG << "setAudioVolumn nVal:" << nVal; ctAudioPlayer::getInstance().setVolumn(nVal); } } } } } void MainWindow::fullScreen(void *pObject, QJsonObject *pJson) { Q_UNUSED(pJson) MainWindow* pWin = (MainWindow*)pObject; emit pWin->sig_fullscreen(true); pWin->slot_fullscreen(true); } void MainWindow::StopMedia(void *pObject, QJsonObject *pJson) { MainWindow* pWin = (MainWindow*)pObject; if(pJson->contains("id")) { QJsonValue id = pJson->value("id"); if(id.isDouble()) { pWin->m_stWebParam.nID = id.toVariant().toInt(); } } if(pJson->contains("info")) { QJsonValue value = pJson->value("info"); if(value.isObject()) { QJsonObject obj = value.toObject(); if(obj.contains("winIndex")) { QJsonValue value = obj.value("winIndex"); if(value.isDouble()) { pWin->m_stWebParam.nWinIndex = value.toVariant().toInt(); } } if(obj.contains("isAll")) { QJsonValue value = obj.value("isAll"); if(value.isBool()) { pWin->m_stWebParam.bAllStop = value.toVariant().toBool(); } } } } if(pWin->m_stWebParam.bAllStop) emit pWin->sig_stopAll(); else { if(pWin->m_stWebParam.nWinIndex >= 0 && pWin->m_stWebParam.nWinIndex < MAX_MEDIA_NUM) { emit pWin->sig_stopOne(pWin->m_stWebParam.nWinIndex); } } QJsonObject dataObject; QJsonObject json; json.insert("code", pWin->m_stWebParam.nCode); json.insert("data", QJsonValue(dataObject)); json.insert("id", pWin->m_stWebParam.nID); json.insert("session", pWin->m_stWebParam.nSession); json.insert("success", "true"); pWin->SendJsonData(json); } void MainWindow::parseJson(QString sData) { QJsonParseError jError; QJsonDocument jDoc = QJsonDocument::fromJson(sData.toLatin1(), &jError);//转换成文档对象 if(!jDoc.isNull() && jError.error == QJsonParseError::NoError) { if(jDoc.isObject()) { QJsonObject object = jDoc.object(); if(object.contains("method")) { QJsonValue sMethod = object.value("method"); if(sMethod.isString()) { QString strMethod = sMethod.toString(); QHashMethodFunIterator iter = m_hashFun.begin(); for(; iter != m_hashFun.end(); ++iter) { QString sKey = iter.key(); if(sKey.contains(strMethod)) { MethodFun fun = m_hashFun.value(sKey); fun(this, &object); } } } } } } } void MainWindow::keyPressEvent(QKeyEvent *event) { if(event->key() == Qt::Key_Escape) { if(m_bFullScreen == true) { emit sig_fullscreen(false); slot_fullscreen(false); event->accept(); } } return QMainWindow::keyPressEvent(event); } void MainWindow::sendWinSelect() { QJsonObject infoObject; infoObject.insert("wndIndex", m_stWebParam.nWinIndex); infoObject.insert("hwnd", m_stWebParam.nHwnd); QJsonObject json; json.insert("method", "window.clicked"); json.insert("info", QJsonValue(infoObject)); json.insert("session", m_stWebParam.nSession); json.insert("success", "true"); SendJsonData(json); } void MainWindow::showFullScreen() { m_normalGeo = this->geometry(); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) QScreen *desk = qApp->screenAt(QCursor::pos()); QRect rect = desk->availableGeometry(); #else QDesktopWidget *desk = qApp->desktop(); int ScreenNum = desk->screenNumber(QCursor::pos()); QRect rect = desk->availableGeometry(ScreenNum); #endif setGeometry(rect + QMargins(32,17,17,17)); QMainWindow::show(); } void MainWindow::showNormal(const QRect &rect) { if(rect.isNull()) { if(m_normalGeo.isNull()) { m_normalGeo = geometry(); m_normalGeo.setWidth(geometry().width()/2); m_normalGeo.setHeight(geometry().height()/2); } if(m_normalGeo.width() > width() || m_normalGeo.height() > height()) { m_normalGeo.setWidth(this->width()/2); m_normalGeo.setHeight(this->height()/2); } if(m_normalGeo.y() < 0) m_normalGeo.setY(0); setGeometry(m_normalGeo); } else { setGeometry(m_normalGeo); } m_bFullScreen = false; QMainWindow::show(); } void MainWindow::on_comboBox_ChangeVideo_activated(int index) { emit sig_setScreenType(index); } void MainWindow::on_newConnection() { m_pSocket = m_pWebSocketServer->nextPendingConnection(); connect(m_pSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(on_processTextMessage(QString))); connect(m_pSocket, SIGNAL(disconnected()), this, SLOT(on_socketDisconnected())); QString item = m_pSocket->peerAddress().toString(); MY_DEBUG << item; m_clientsList << m_pSocket; } void MainWindow::on_socketDisconnected() { } void MainWindow::on_processTextMessage(QString message) { QString time = m_pCurrentDateTime->currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz ddd"); QString item = m_pSocket->peerAddress().toString(); //MY_DEBUG << time + "" + item + " " + message; parseJson(message); } void MainWindow::on_pushButton_PrevPage_clicked() { emit sig_prevPage(); } void MainWindow::on_pushButton_NextPage_clicked() { emit sig_nextPage(); } void MainWindow::slot_curWinIndex(int nIndex) { m_stWebParam.nWinIndex = nIndex; sendWinSelect(); } void MainWindow::slot_playFailTip(QString sTip) { QMessageBox::critical(this, "myFFmpeg", sTip); } void MainWindow::slot_setPageInfo(QString sInfo) { ui->label_PageNumber->setText(sInfo); } void MainWindow::slot_fullscreen(bool bFull) { if(bFull) { m_bFullScreen = true; ui->widget_control->hide(); showFullScreen(); } else { m_bFullScreen = false; ui->widget_control->show(); showNormal(); } }