From 492bcca9b5024e05586e7fdd68e98527542a0d66 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 25 Oct 2021 01:24:08 +0800 Subject: [PATCH] no message --- client/webrtc_demo/src/mainwindow.cpp | 117 +++++++++++++++++++---- client/webrtc_demo/src/mainwindow.h | 22 ++++- client/webrtc_demo/src/mainwindow.ui | 9 +- client/webrtc_demo/src/signal_client.cpp | 22 +++++ client/webrtc_demo/src/signal_client.h | 2 + 5 files changed, 149 insertions(+), 23 deletions(-) diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index 0a77343..18e25b6 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -15,6 +15,7 @@ MainWindow::MainWindow(QWidget *parent) ,mHandler(new rtc::RefCountedObject()) ,mSignalClient(nullptr) ,mModel(nullptr) + ,mCalling(false) { ui->setupUi(this); ui->openGLWidget->SetImgSize(640,480); @@ -26,6 +27,11 @@ MainWindow::MainWindow(QWidget *parent) GetUserName((TCHAR*)username, &size); mModel = new QStandardItemModel(this); 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() @@ -112,9 +118,6 @@ int WebrtcHanlder::InitWebrtc() qDebug()<< "OpenVideoCaptureDevice failed"; } - - m_peer_connection_->CreateOffer(this, - webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); } @@ -152,10 +155,67 @@ int WebrtcHanlder::AddTrack() } 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(); + } + 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 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() { @@ -211,25 +271,11 @@ void WebrtcHanlder::OnIceConnectionReceivingChange(bool receiving) { } -class DummySetSessionDescriptionObserver - : public webrtc::SetSessionDescriptionObserver { -public: - static DummySetSessionDescriptionObserver* Create() { - return new rtc::RefCountedObject(); - } - 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 kSessionDescriptionSdpName[] = "sdp"; void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc) { qDebug()<type().c_str(); - std::string sdp; desc->ToString(&sdp); @@ -239,6 +285,9 @@ void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc) qDebug()<<"sdp : \r\n "<SetLocalDescription( DummySetSessionDescriptionObserver::Create(), desc); + if(QString(desc->type().c_str()) == "offer"){ + OnOfferSdp(QJsonDocument(addr).toJson()); + } } 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(response(int ,QJsonObject)), this,SLOT( signal_response(int,QJsonObject))); - + mHandler->SetSignalClient(this->mSignalClient); } void MainWindow::signal_conneted() { ui->label_5->setText("信令服务器已连接"); this->mSignalClient->SendLogin(); + ui->label_6->setText(QString("本节点名称: " + mSignalClient->PeerName())); } void MainWindow::on_pushButton_2_clicked() @@ -283,11 +333,40 @@ void MainWindow::signal_response(int type,QJsonObject data) qDebug()<mModel->clear(); for (auto itr = data.begin();itr != data.end();itr++){ auto item = new QStandardItem(itr.key()); item->setEditable(false); mModel->appendRow(item); } 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()<item(index.row())->text(); + mRemoteName = mModel->item(index.row())->text(); + mCalling = true; + +} + +void MainWindow::on_local_sdp(QString sdp) +{ + qDebug()<<"sdp size is "<SendSDPOffer(mRemoteName,sdp); +} diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index e3fc966..4ce576c 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -35,11 +35,21 @@ namespace Ui { class MainWindow; } QT_END_NAMESPACE -class WebrtcHanlder :public webrtc::PeerConnectionObserver, +class WebrtcHanlder :public QObject, + public webrtc::PeerConnectionObserver, public webrtc::CreateSessionDescriptionObserver{ + Q_OBJECT + +signals: + void OnOfferSdp(QString); + void OnAnswerSdp(QString); public: int InitWebrtc(); int AddTrack(); + void SetSignalClient(SignalClient *); + void CreateOffer(); + void SetRemoteSdp(QString); + void CreateAnwer(); protected: ~WebrtcHanlder(); // @@ -61,7 +71,6 @@ protected: webrtc::PeerConnectionInterface::IceGatheringState new_state) override; void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; void OnIceConnectionReceivingChange(bool receiving) override; - // CreateSessionDescriptionObserver void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; void OnFailure(webrtc::RTCError error) override; @@ -70,7 +79,7 @@ private: rtc::scoped_refptr m_peer_connection_; rtc::scoped_refptr m_peer_connection_factory_; - + SignalClient *mClient; }; @@ -92,10 +101,17 @@ private slots: void on_pushButton_2_clicked(); void signal_closed(); void signal_response(int,QJsonObject); + void itemClicked(QModelIndex); + void on_local_sdp(QString); private: Ui::MainWindow *ui; rtc::scoped_refptr mHandler; SignalClient *mSignalClient; QStandardItemModel *mModel; + QString mPeerName; + QString mRemoteName; + QString mRemoteNameCalled; + bool mCalling; + }; #endif // MAINWINDOW_H diff --git a/client/webrtc_demo/src/mainwindow.ui b/client/webrtc_demo/src/mainwindow.ui index 80f1658..080696d 100644 --- a/client/webrtc_demo/src/mainwindow.ui +++ b/client/webrtc_demo/src/mainwindow.ui @@ -133,7 +133,14 @@ - + + + + + 本机节点: + + + diff --git a/client/webrtc_demo/src/signal_client.cpp b/client/webrtc_demo/src/signal_client.cpp index 31e336e..df2695a 100644 --- a/client/webrtc_demo/src/signal_client.cpp +++ b/client/webrtc_demo/src/signal_client.cpp @@ -76,6 +76,27 @@ void SignalClient::SetURL(QString 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()<