对接加入房间,节点枚举

master
DESKTOP-4RNDQIC\29019 2021-10-24 00:05:50 +08:00
parent 8dd4ce9f79
commit 062cd9efde
5 changed files with 265 additions and 30 deletions

View File

@ -3,19 +3,29 @@
#include <QDebug>
#include <QMessageBox>
#include "MyCapturer.h"
#include <QThread>
#include <windows.h>
#include <Lmcons.h>
#include <QAction>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
,mHandler(new rtc::RefCountedObject<WebrtcHanlder>())
,mSignalClient(nullptr)
,mModel(nullptr)
{
ui->setupUi(this);
ui->openGLWidget->SetImgSize(640,480);
ui->openGLWidget->show();
mHandler->InitWebrtc();
mSignalClient = new SignalClient(QUrl("ws://127.0.0.1:9555/ws"),true,this);
TCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName((TCHAR*)username, &size);
mModel = new QStandardItemModel(this);
ui->treeView->setModel(mModel);
}
MainWindow::~MainWindow()
@ -237,3 +247,47 @@ void WebrtcHanlder::OnFailure(webrtc::RTCError error)
}
void MainWindow::on_pushButton_clicked()
{
mSignalClient = new SignalClient(QUrl("ws://127.0.0.1:9555/ws"),true,this);
connect(this->mSignalClient,SIGNAL(connected()),this,SLOT(signal_conneted()));
connect(this->mSignalClient,SIGNAL(response(int ,QJsonObject)),
this,SLOT( signal_response(int,QJsonObject)));
}
void MainWindow::signal_conneted()
{
ui->label_5->setText("信令服务器已连接");
this->mSignalClient->SendLogin();
}
void MainWindow::on_pushButton_2_clicked()
{
if(ui->lineEdit->text() != ""){
if(mSignalClient->Connected()){
this->mSignalClient->SendInRoom(ui->lineEdit->text());
this->mSignalClient->SendListRoom(ui->lineEdit->text());
}
}
}
void MainWindow::signal_closed()
{
ui->label_5->setText("信令服务器已断开");
}
void MainWindow::signal_response(int type,QJsonObject data)
{
qDebug()<<type<<data;
switch (type) {
case 2004:
for (auto itr = data.begin();itr != data.end();itr++){
auto item = new QStandardItem(itr.key());
item->setEditable(false);
mModel->appendRow(item);
}
break;
}
}

View File

@ -28,6 +28,7 @@
#include <QMainWindow>
#include "api/video/i420_buffer.h"
#include "signal_client.h"
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -85,9 +86,16 @@ public slots:
void OnUpdateFrame( rtc::scoped_refptr<webrtc::I420BufferInterface>& buffer);
void OnUpdateFrame1( uint8_t *);
private slots:
void on_pushButton_clicked();
void signal_conneted();
void on_pushButton_2_clicked();
void signal_closed();
void signal_response(int,QJsonObject);
private:
Ui::MainWindow *ui;
rtc::scoped_refptr<WebrtcHanlder> mHandler;
SignalClient *mSignalClient;
QStandardItemModel *mModel;
};
#endif // MAINWINDOW_H

View File

@ -14,46 +14,134 @@
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,12">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,15,0">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0,1,0,5">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0,1,0,2,0,0,1,0">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>摄像头:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox"/>
<widget class="QComboBox" name="comboBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>麦克风:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_2"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<widget class="QComboBox" name="comboBox_2">
<property name="minimumSize">
<size>
<width>40</width>
<height>20</height>
<width>0</width>
<height>25</height>
</size>
</property>
</spacer>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>服务器地址:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_2">
<property name="text">
<string>ws://127.0.0.1:9555/ws</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>连接信令服务器</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>房间号:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>加入房间</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="CPlayWidget" name="openGLWidget"/>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="2,9">
<item>
<widget class="QTreeView" name="treeView"/>
</item>
<item>
<widget class="CPlayWidget" name="openGLWidget">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>信令服务器未连接</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>

View File

@ -1,16 +1,79 @@
#include "signal_client.h"
#include <QJsonDocument>
#include <windows.h>
#include <Lmcons.h>
SignalClient::SignalClient(const QUrl &url, bool debug, QObject *parent):
QObject(parent),
m_url(url),
m_debug(debug)
m_debug(debug),
m_connected(false)
{
qDebug() << "WebSocket server:" << url;
connect(&m_webSocket, &QWebSocket::connected, this, &SignalClient::onConnected);
connect(&m_webSocket, &QWebSocket::disconnected, this, &SignalClient::onUnConnected);
m_webSocket.open(QUrl(url));
connect(&m_timer,SIGNAL(timeout()),this,SLOT(onReconenectTimeout()));
m_timer.stop();
m_timer.setSingleShot(false);
}
SignalClient::~SignalClient()
{
m_timer.stop();
m_webSocket.close();
}
void SignalClient::SendLogin()
{
TCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName((TCHAR*)username, &size);
QJsonObject obj;
QJsonObject data;
QJsonDocument doc;
obj.insert("type",1000);
data.insert("name",QString::fromWCharArray(username));
obj.insert("data",data);
m_peer_name = QString::fromWCharArray(username);
m_webSocket.sendTextMessage(QJsonDocument(obj).toJson());
}
#define REQ_INROOM 1001
void SignalClient::SendInRoom(QString room)
{
QJsonObject obj;
QJsonObject data;
data.insert("name",m_peer_name);
data.insert("room_name",room);
obj.insert("type",REQ_INROOM);
obj.insert("data",data);
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson()));
}
#define REQ_INROOM 1004
void SignalClient::SendListRoom(QString room)
{
QJsonObject obj;
QJsonObject data;
data.insert("room_name",room);
obj.insert("type",REQ_INROOM);
obj.insert("data",data);
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson()));
}
bool SignalClient::Connected()
{
return m_connected;
}
void SignalClient::SetURL(QString url)
{
m_url = url;
}
void SignalClient::onConnected()
@ -18,28 +81,34 @@ void SignalClient::onConnected()
qDebug() << "WebSocket connected";
connect(&m_webSocket, &QWebSocket::textMessageReceived,
this, &SignalClient::onTextMessageReceived);
QJsonObject addr;
QJsonObject dat;
dat.insert("message","hello world");
addr.insert("type", 1001);
addr.insert("data", dat);
m_webSocket.sendTextMessage(QJsonDocument(addr).toJson());
m_connected = true;
m_timer.stop();
connected();
}
void SignalClient::onUnConnected()
{
qDebug() <<"unconnected";
m_timer.start(1000);
m_timer.setSingleShot(false);
m_webSocket.open(QUrl(m_url));
closed();
}
void SignalClient::onTextMessageReceived(QString message)
{
qDebug() << "Message received:" << message;
m_webSocket.close();
auto obj = QJsonDocument::fromJson(message.toUtf8()).object();
qDebug()<<obj["type"];
response(obj["type"].toInt(),obj["data"].toObject());
}
void SignalClient::onSocketError(QAbstractSocket::SocketError error)
{
qDebug()<<error;
}
void SignalClient::onReconenectTimeout()
{
qDebug()<<"timeout";
}

View File

@ -5,26 +5,42 @@
#include <QObject>
#include <QJsonObject>
#include <QJsonDocument>
#include <QTimer>
class SignalClient : public QObject
{
Q_OBJECT
public:
explicit SignalClient(const QUrl &url, bool debug = false, QObject *parent = Q_NULLPTR);
explicit SignalClient(const QUrl &url, bool debug = false,
QObject *parent = Q_NULLPTR);
~SignalClient();
Q_SIGNALS:
void closed();
void connected();
void response(int type,QJsonObject);
public:
void SendLogin();
void SendInRoom(QString );
void SendListRoom(QString);
bool Connected();
void SetURL(QString);
private Q_SLOTS:
void onConnected();
void onUnConnected();
void onTextMessageReceived(QString message);
void onSocketError(QAbstractSocket::SocketError error);
void onReconenectTimeout();
private:
QString m_room_name; // 所属房间名称
QString m_peer_name; // 节点名称
QWebSocket m_webSocket;
QUrl m_url;
bool m_debug;
bool m_connected;
QTimer m_timer;
};
#endif // SIGNALCLIENT_H