no message

master
zcy 2021-10-25 01:24:08 +08:00
parent 062cd9efde
commit 492bcca9b5
5 changed files with 149 additions and 23 deletions

View File

@ -15,6 +15,7 @@ MainWindow::MainWindow(QWidget *parent)
,mHandler(new rtc::RefCountedObject<WebrtcHanlder>()) ,mHandler(new rtc::RefCountedObject<WebrtcHanlder>())
,mSignalClient(nullptr) ,mSignalClient(nullptr)
,mModel(nullptr) ,mModel(nullptr)
,mCalling(false)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->openGLWidget->SetImgSize(640,480); ui->openGLWidget->SetImgSize(640,480);
@ -26,6 +27,11 @@ MainWindow::MainWindow(QWidget *parent)
GetUserName((TCHAR*)username, &size); GetUserName((TCHAR*)username, &size);
mModel = new QStandardItemModel(this); mModel = new QStandardItemModel(this);
ui->treeView->setModel(mModel); ui->treeView->setModel(mModel);
connect(ui->treeView,SIGNAL(doubleClicked(QModelIndex)),
this,SLOT(itemClicked(QModelIndex)));
connect((WebrtcHanlder*)(mHandler.get()),SIGNAL(OnOfferSdp(QString)),
this,SLOT(on_local_sdp(QString)));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -112,9 +118,6 @@ int WebrtcHanlder::InitWebrtc()
qDebug()<< "OpenVideoCaptureDevice failed"; qDebug()<< "OpenVideoCaptureDevice failed";
} }
m_peer_connection_->CreateOffer(this,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
} }
@ -152,10 +155,67 @@ int WebrtcHanlder::AddTrack()
} }
webrtc::DataChannelInit config; webrtc::DataChannelInit config;
// DataChannelの設定
} }
void WebrtcHanlder::SetSignalClient(SignalClient * cli)
{
this->mClient = cli;
}
void WebrtcHanlder::CreateOffer()
{
if(nullptr != m_peer_connection_){
m_peer_connection_->CreateOffer(this,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
}
}
class DummySetSessionDescriptionObserver
: public webrtc::SetSessionDescriptionObserver {
public:
static DummySetSessionDescriptionObserver* Create() {
return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
}
virtual void OnSuccess() { RTC_LOG(INFO) << __FUNCTION__; }
virtual void OnFailure(webrtc::RTCError error) {
RTC_LOG(INFO) << __FUNCTION__ << " " << ToString(error.type()) << ": "
<< error.message();
}
};
void WebrtcHanlder::SetRemoteSdp(QString sdp)
{
qDebug()<<"recv" << sdp;
auto doc = QJsonDocument::fromJson(sdp.toUtf8());
auto obj = doc.object();
webrtc::SdpParseError error;
std::unique_ptr<webrtc::SessionDescriptionInterface> session_description =
webrtc::CreateSessionDescription(webrtc::SdpType::kOffer, sdp.toStdString(), &error);
if (!session_description) {
RTC_LOG(WARNING) << "Can't parse received session description message. "
<< "SdpParseError was: " << error.description;
return;
}
if(nullptr != m_peer_connection_){
m_peer_connection_->SetRemoteDescription(
DummySetSessionDescriptionObserver::Create(),
session_description.release());;
}
}
void WebrtcHanlder::CreateAnwer()
{
m_peer_connection_->CreateAnswer(
this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
}
WebrtcHanlder::~WebrtcHanlder() WebrtcHanlder::~WebrtcHanlder()
{ {
@ -211,25 +271,11 @@ void WebrtcHanlder::OnIceConnectionReceivingChange(bool receiving) {
} }
class DummySetSessionDescriptionObserver
: public webrtc::SetSessionDescriptionObserver {
public:
static DummySetSessionDescriptionObserver* Create() {
return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
}
virtual void OnSuccess() { RTC_LOG(INFO) << __FUNCTION__; }
virtual void OnFailure(webrtc::RTCError error) {
RTC_LOG(INFO) << __FUNCTION__ << " " << ToString(error.type()) << ": "
<< error.message();
}
};
const char kSessionDescriptionTypeName[] = "type"; const char kSessionDescriptionTypeName[] = "type";
const char kSessionDescriptionSdpName[] = "sdp"; const char kSessionDescriptionSdpName[] = "sdp";
void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc) void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc)
{ {
qDebug()<<desc->type().c_str(); qDebug()<<desc->type().c_str();
std::string sdp; std::string sdp;
desc->ToString(&sdp); desc->ToString(&sdp);
@ -239,6 +285,9 @@ void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc)
qDebug()<<"sdp : \r\n "<<QJsonDocument(addr).toJson(); qDebug()<<"sdp : \r\n "<<QJsonDocument(addr).toJson();
m_peer_connection_->SetLocalDescription( m_peer_connection_->SetLocalDescription(
DummySetSessionDescriptionObserver::Create(), desc); DummySetSessionDescriptionObserver::Create(), desc);
if(QString(desc->type().c_str()) == "offer"){
OnOfferSdp(QJsonDocument(addr).toJson());
}
} }
void WebrtcHanlder::OnFailure(webrtc::RTCError error) void WebrtcHanlder::OnFailure(webrtc::RTCError error)
@ -254,13 +303,14 @@ void MainWindow::on_pushButton_clicked()
connect(this->mSignalClient,SIGNAL(connected()),this,SLOT(signal_conneted())); connect(this->mSignalClient,SIGNAL(connected()),this,SLOT(signal_conneted()));
connect(this->mSignalClient,SIGNAL(response(int ,QJsonObject)), connect(this->mSignalClient,SIGNAL(response(int ,QJsonObject)),
this,SLOT( signal_response(int,QJsonObject))); this,SLOT( signal_response(int,QJsonObject)));
mHandler->SetSignalClient(this->mSignalClient);
} }
void MainWindow::signal_conneted() void MainWindow::signal_conneted()
{ {
ui->label_5->setText("信令服务器已连接"); ui->label_5->setText("信令服务器已连接");
this->mSignalClient->SendLogin(); this->mSignalClient->SendLogin();
ui->label_6->setText(QString("本节点名称: " + mSignalClient->PeerName()));
} }
void MainWindow::on_pushButton_2_clicked() void MainWindow::on_pushButton_2_clicked()
@ -283,11 +333,40 @@ void MainWindow::signal_response(int type,QJsonObject data)
qDebug()<<type<<data; qDebug()<<type<<data;
switch (type) { switch (type) {
case 2004: case 2004:
this->mModel->clear();
for (auto itr = data.begin();itr != data.end();itr++){ for (auto itr = data.begin();itr != data.end();itr++){
auto item = new QStandardItem(itr.key()); auto item = new QStandardItem(itr.key());
item->setEditable(false); item->setEditable(false);
mModel->appendRow(item); mModel->appendRow(item);
} }
break; break;
case 2005:
auto sdp = data["sdp"].toString();
auto remote_name = data["remote_name"].toString();
this->mHandler.get()->SetRemoteSdp(sdp);
mRemoteName = remote_name;
if(!mCalling)
mHandler->CreateAnwer();
break;
} }
} }
void MainWindow::itemClicked(QModelIndex index)
{
if(!mSignalClient->Connected()){
qDebug()<<"请先连接信令服务";
}
mHandler->CreateOffer();
//mSignalClient->SendSDPOffer();
qDebug()<<mModel->item(index.row())->text();
mRemoteName = mModel->item(index.row())->text();
mCalling = true;
}
void MainWindow::on_local_sdp(QString sdp)
{
qDebug()<<"sdp size is "<<sdp.size();
mSignalClient->SendSDPOffer(mRemoteName,sdp);
}

View File

@ -35,11 +35,21 @@ namespace Ui { class MainWindow; }
QT_END_NAMESPACE QT_END_NAMESPACE
class WebrtcHanlder :public webrtc::PeerConnectionObserver, class WebrtcHanlder :public QObject,
public webrtc::PeerConnectionObserver,
public webrtc::CreateSessionDescriptionObserver{ public webrtc::CreateSessionDescriptionObserver{
Q_OBJECT
signals:
void OnOfferSdp(QString);
void OnAnswerSdp(QString);
public: public:
int InitWebrtc(); int InitWebrtc();
int AddTrack(); int AddTrack();
void SetSignalClient(SignalClient *);
void CreateOffer();
void SetRemoteSdp(QString);
void CreateAnwer();
protected: protected:
~WebrtcHanlder(); ~WebrtcHanlder();
// //
@ -61,7 +71,6 @@ protected:
webrtc::PeerConnectionInterface::IceGatheringState new_state) override; webrtc::PeerConnectionInterface::IceGatheringState new_state) override;
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
void OnIceConnectionReceivingChange(bool receiving) override; void OnIceConnectionReceivingChange(bool receiving) override;
// CreateSessionDescriptionObserver // CreateSessionDescriptionObserver
void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
void OnFailure(webrtc::RTCError error) override; void OnFailure(webrtc::RTCError error) override;
@ -70,7 +79,7 @@ private:
rtc::scoped_refptr<webrtc::PeerConnectionInterface> m_peer_connection_; rtc::scoped_refptr<webrtc::PeerConnectionInterface> m_peer_connection_;
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
m_peer_connection_factory_; m_peer_connection_factory_;
SignalClient *mClient;
}; };
@ -92,10 +101,17 @@ private slots:
void on_pushButton_2_clicked(); void on_pushButton_2_clicked();
void signal_closed(); void signal_closed();
void signal_response(int,QJsonObject); void signal_response(int,QJsonObject);
void itemClicked(QModelIndex);
void on_local_sdp(QString);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
rtc::scoped_refptr<WebrtcHanlder> mHandler; rtc::scoped_refptr<WebrtcHanlder> mHandler;
SignalClient *mSignalClient; SignalClient *mSignalClient;
QStandardItemModel *mModel; QStandardItemModel *mModel;
QString mPeerName;
QString mRemoteName;
QString mRemoteNameCalled;
bool mCalling;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -133,7 +133,14 @@
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>本机节点:</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="label_5"> <widget class="QLabel" name="label_5">
<property name="text"> <property name="text">

View File

@ -76,6 +76,27 @@ void SignalClient::SetURL(QString url)
m_url = url; m_url = url;
} }
#define REQ_SDP 1005
void SignalClient::SendSDPOffer(QString peer, QString sdp)
{
QJsonObject obj;
QJsonObject data;
data.insert("sdp",sdp);
data.insert("name",m_peer_name);
data.insert("remote_name",peer);
obj.insert("type",REQ_SDP);
obj.insert("data",data);
qDebug()<<QString(QJsonDocument(obj).toJson());
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson()));
qDebug()<<QString(QJsonDocument(obj).toJson());
}
QString SignalClient::PeerName()
{
return m_peer_name;
}
void SignalClient::onConnected() void SignalClient::onConnected()
{ {
qDebug() << "WebSocket connected"; qDebug() << "WebSocket connected";
@ -112,3 +133,4 @@ void SignalClient::onReconenectTimeout()
{ {
qDebug()<<"timeout"; qDebug()<<"timeout";
} }

View File

@ -25,6 +25,8 @@ public:
void SendListRoom(QString); void SendListRoom(QString);
bool Connected(); bool Connected();
void SetURL(QString); void SetURL(QString);
void SendSDPOffer(QString peer,QString sdp);
QString PeerName();
private Q_SLOTS: private Q_SLOTS:
void onConnected(); void onConnected();
void onUnConnected(); void onUnConnected();