From cc659c89d652806ade6686fb1f6a6a7e3e439188 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sat, 11 Dec 2021 23:27:41 +0800 Subject: [PATCH] no message --- client/webrtc_demo/src/MyCapturer.cpp | 1 + client/webrtc_demo/src/main.cpp | 9 +--- client/webrtc_demo/src/mainwindow.cpp | 66 ++++++++++++++++++++++----- client/webrtc_demo/src/mainwindow.h | 15 +++++- client/webrtc_demo/src/mainwindow.ui | 31 +++++++++++-- 5 files changed, 97 insertions(+), 25 deletions(-) diff --git a/client/webrtc_demo/src/MyCapturer.cpp b/client/webrtc_demo/src/MyCapturer.cpp index e362a45..18f33a8 100644 --- a/client/webrtc_demo/src/MyCapturer.cpp +++ b/client/webrtc_demo/src/MyCapturer.cpp @@ -15,6 +15,7 @@ void MyCapturer::startCapturer() { capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options); capturer_->Start(this); CaptureFrame(); + } webrtc::MediaSourceInterface::SourceState MyCapturer::state() const { diff --git a/client/webrtc_demo/src/main.cpp b/client/webrtc_demo/src/main.cpp index 786bbbd..0e320a5 100644 --- a/client/webrtc_demo/src/main.cpp +++ b/client/webrtc_demo/src/main.cpp @@ -99,9 +99,9 @@ void InitCustomMetaType(){ qRegisterMetaType("uint8_t*"); qRegisterMetaType("uint64_t"); qRegisterMetaType("uint32_t"); - - } + + int main(int argc, char *argv[]) { InitCustomMetaType(); @@ -118,11 +118,6 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); MainWindow w; -// QObject::connect((VcmCapturerTest*)capturer.get(),SIGNAL(UpdateFrame(rtc::scoped_refptr&)),&w, -// SLOT(OnUpdateFrame( rtc::scoped_refptr&)),Qt::ConnectionType::QueuedConnection); -// QObject::connect((VcmCapturerTest*)capturer.get(),SIGNAL(UpdateFrame1(uint8_t*)),&w, -// SLOT(OnUpdateFrame1( uint8_t *)),Qt::ConnectionType::QueuedConnection); - w.show(); return a.exec(); } diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index eed0f92..1084b38 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -58,15 +58,18 @@ MainWindow::MainWindow(QWidget *parent) ,mSignalClient(nullptr) ,mModel(nullptr) ,mCalling(false) - ,mRemoteStream(nullptr) + ,mRemoteVideoTrack(nullptr) + ,mLocalVideoTrack(nullptr) { ui->setupUi(this); ui->openGLWidget->show(); mHandler->InitWebrtc(); + this->mLocalVideoTrack = mHandler->VideoTrack(); TCHAR username[UNLEN + 1]; DWORD size = UNLEN + 1; GetUserName((TCHAR*)username, &size); + this->mPeerName = QString::fromWCharArray(username); mModel = new QStandardItemModel(this); ui->treeView->setModel(mModel); connect(ui->treeView,SIGNAL(doubleClicked(QModelIndex)), @@ -92,6 +95,11 @@ void WebrtcHanlder::SetRemotePeerName(QString remote) this->mRemoteName = remote; } +WebrtcHanlder::WebrtcHanlder() +{ + mVideoTrack = nullptr; +} + int WebrtcHanlder::InitWebrtc() { m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( @@ -137,6 +145,7 @@ int WebrtcHanlder::InitWebrtc() qDebug() << "Failed to add audio track to PeerConnection: " << result_or_error.error().message(); } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + " m_peer_connection_ AddTrack" + kAudioLabel).toStdString()); rtc::scoped_refptr video_device = new rtc::RefCountedObject(); @@ -146,6 +155,8 @@ int WebrtcHanlder::InitWebrtc() m_peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device)); qDebug()<<"local track is "<id().c_str(); result_or_error = m_peer_connection_->AddTrack(video_track_, { kStreamId }); + mVideoTrack = video_track_.get(); + video_track_->AddRef(); if (!result_or_error.ok()) { qDebug() << "Failed to add video track to PeerConnection: " << result_or_error.error().message(); @@ -253,6 +264,11 @@ void WebrtcHanlder::CreateAnwer() " CreateAnswer ").toStdString()); } +webrtc::VideoTrackInterface *WebrtcHanlder::VideoTrack() +{ + return mVideoTrack; +} + WebrtcHanlder::~WebrtcHanlder() { @@ -291,6 +307,15 @@ void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceCo gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + " OnIceConnectionChange " + IceConnectionStateName(new_state)).toStdString()); + if((new_state == + webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionDisconnected)){ + m_peer_connection_->Close(); + OnConectionDisconnected(); + } + if((new_state == + webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionCompleted)){ + OnConectionComplete(); + } } void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) { @@ -434,13 +459,14 @@ void MainWindow::itemClicked(QModelIndex index) if(!mSignalClient->Connected()){ qDebug()<<"请先连接信令服务"; } - mHandler->CreateOffer(); - //mSignalClient->SendSDPOffer(); qDebug()<item(index.row())->text(); mRemoteName = mModel->item(index.row())->text(); + if(mRemoteName == this->mPeerName){ + return; + } + mHandler->CreateOffer(); mHandler->SetRemotePeerName(mRemoteName); mCalling = true; - } void MainWindow::on_local_sdp(QString sdp) @@ -452,12 +478,12 @@ void MainWindow::on_local_sdp(QString sdp) void MainWindow::on_track_add(webrtc::MediaStreamTrackInterface *data) { qDebug()<<"on_track_add"<kind().c_str(); - mRemoteStream = reinterpret_cast(data); + mRemoteVideoTrack = reinterpret_cast(data); - if (mRemoteStream->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) { - auto* video_track = static_cast(mRemoteStream); - qDebug()<<"remote trackid is "<id().c_str(); - remote_renderer_.reset(new VideoRenderer( 1, 1, video_track)); + if (mRemoteVideoTrack->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) { + mRemoteVideoTrack = static_cast(mRemoteVideoTrack); + qDebug()<<"remote trackid is "<id().c_str(); + remote_renderer_.reset(new VideoRenderer( 1, 1, mRemoteVideoTrack)); connect((VideoRenderer*)(remote_renderer_.get()), SIGNAL(UpdateFrame(rtc::scoped_refptr&)), @@ -620,13 +646,29 @@ void VideoRenderer::SetSize(int width, int height) void MainWindow::on_pushButton_3_clicked() { - if(mRemoteStream != nullptr){ - mRemoteStream->set_enabled(false); + if(mRemoteVideoTrack != nullptr){ + mRemoteVideoTrack->set_enabled(false); } } void MainWindow::on_pushButton_4_clicked() { - + if(mLocalVideoTrack != nullptr){ + mLocalVideoTrack->set_enabled(false); + } +} + +void MainWindow::on_pushButton_5_clicked() +{ + if(mRemoteVideoTrack != nullptr){ + mRemoteVideoTrack->set_enabled(false); + } +} + +void MainWindow::on_pushButton_6_clicked() +{ + if(mLocalVideoTrack != nullptr){ + mLocalVideoTrack->set_enabled(true); + } } diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index e1248d9..af68149 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -96,8 +96,11 @@ class WebrtcHanlder :public QObject, signals: void OnOfferSdp(QString); void OnRemoteTrack(webrtc::MediaStreamTrackInterface*); + void OnConectionDisconnected(); + void OnConectionComplete(); public: + WebrtcHanlder(); int InitWebrtc(); void SetSignalClient(SignalClient *); void CreateOffer(); @@ -106,6 +109,7 @@ public: void SetRemoteSdp(QString); void SetRemoteCandidate(QString); void CreateAnwer(); + webrtc::VideoTrackInterface *VideoTrack(); protected: ~WebrtcHanlder(); // @@ -137,6 +141,7 @@ private: m_peer_connection_factory_; SignalClient *mClient; QString mRemoteName; + webrtc::VideoTrackInterface *mVideoTrack; }; class MainWindow :public QMainWindow @@ -159,6 +164,12 @@ private slots: void on_notify_size(uint32_t,uint32_t); void on_pushButton_3_clicked(); + void on_pushButton_4_clicked(); + + void on_pushButton_5_clicked(); + + void on_pushButton_6_clicked(); + private: Ui::MainWindow *ui; rtc::scoped_refptr mHandler; @@ -169,6 +180,8 @@ private: bool mCalling; std::unique_ptr local_renderer_; std::unique_ptr remote_renderer_; - webrtc::MediaStreamTrackInterface* mRemoteStream; + webrtc::VideoTrackInterface* mRemoteVideoTrack; + webrtc::VideoTrackInterface* mLocalVideoTrack; + }; #endif // MAINWINDOW_H diff --git a/client/webrtc_demo/src/mainwindow.ui b/client/webrtc_demo/src/mainwindow.ui index 1831f3f..2fa6b9b 100644 --- a/client/webrtc_demo/src/mainwindow.ui +++ b/client/webrtc_demo/src/mainwindow.ui @@ -73,7 +73,7 @@ - ws://192.168.5.192:9555/ws + ws://192.168.5.73:9555/ws @@ -137,7 +137,7 @@ - + @@ -154,14 +154,28 @@ - 暂停对方视频流 + 暂停对方视频接收 + + + + + + + 恢复对方视频接收 - 暂停本地视频流 + 暂停本地视频流采集 + + + + + + + 恢复本地视频采集 @@ -181,7 +195,7 @@ - + @@ -196,6 +210,13 @@ + + + + + + +