From 9e1a4a28e64e4d4246e5cbe4dc3c5e0ab115663e Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 12 Dec 2021 19:58:36 +0800 Subject: [PATCH] add local track --- client/webrtc_demo/src/mainwindow.cpp | 100 +++++++++++++++++--------- client/webrtc_demo/src/mainwindow.h | 4 ++ 2 files changed, 70 insertions(+), 34 deletions(-) diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index 1084b38..1f012fb 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -64,8 +64,6 @@ MainWindow::MainWindow(QWidget *parent) ui->setupUi(this); ui->openGLWidget->show(); - mHandler->InitWebrtc(); - this->mLocalVideoTrack = mHandler->VideoTrack(); TCHAR username[UNLEN + 1]; DWORD size = UNLEN + 1; GetUserName((TCHAR*)username, &size); @@ -98,10 +96,13 @@ void WebrtcHanlder::SetRemotePeerName(QString remote) WebrtcHanlder::WebrtcHanlder() { mVideoTrack = nullptr; + mAudioTrack = nullptr; + m_peer_connection_factory_ = nullptr; } int WebrtcHanlder::InitWebrtc() { + m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( nullptr /* network_thread */, nullptr /* worker_thread */, nullptr /* signaling_thread */, nullptr /* default_adm */, @@ -125,6 +126,9 @@ int WebrtcHanlder::InitWebrtc() server.uri = "stun:stun.l.google.com:19302"; // stun服务器 config.servers.push_back(server); + if(m_peer_connection_ != nullptr){ + m_peer_connection_.release(); + } m_peer_connection_ = m_peer_connection_factory_->CreatePeerConnection( config, nullptr, nullptr, this); @@ -134,38 +138,7 @@ int WebrtcHanlder::InitWebrtc() x->show(); exit(0); } - - rtc::scoped_refptr audio_track( - m_peer_connection_factory_->CreateAudioTrack( - kAudioLabel, m_peer_connection_factory_->CreateAudioSource( - cricket::AudioOptions()))); - - auto result_or_error = m_peer_connection_->AddTrack(audio_track, {kStreamId}); - if (!result_or_error.ok()) { - 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(); - if (video_device) { - video_device->startCapturer(); - rtc::scoped_refptr video_track_( - 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(); - } - gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + - " m_peer_connection_ AddTrack" + kVideoLabel).toStdString()); - } else { - qDebug()<< "OpenVideoCaptureDevice failed"; - } + AddLocalTrack(); } @@ -269,6 +242,61 @@ webrtc::VideoTrackInterface *WebrtcHanlder::VideoTrack() return mVideoTrack; } +void WebrtcHanlder::AddLocalTrack() +{ + if(mAudioTrack == nullptr){ + rtc::scoped_refptr audio_track( + m_peer_connection_factory_->CreateAudioTrack( + kAudioLabel, m_peer_connection_factory_->CreateAudioSource( + cricket::AudioOptions()))); + + auto result_or_error = m_peer_connection_->AddTrack(audio_track, {kStreamId}); + if (!result_or_error.ok()) { + qDebug() << "Failed to add audio track to PeerConnection: " + << result_or_error.error().message(); + } + mAudioTrack = audio_track.get(); + }else{ + auto result_or_error = m_peer_connection_->AddTrack(mAudioTrack, {kStreamId}); + if (!result_or_error.ok()) { + 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()); + if(mVideoTrack == nullptr){ + rtc::scoped_refptr video_device = new rtc::RefCountedObject(); + if (video_device) { + video_device->startCapturer(); + rtc::scoped_refptr video_track_( + m_peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device)); + qDebug()<<"local track is "<id().c_str(); + auto 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(); + } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " m_peer_connection_ AddTrack" + kVideoLabel).toStdString()); + } else { + qDebug()<< "OpenVideoCaptureDevice failed"; + } + }else{ + auto result_or_error = m_peer_connection_->AddTrack(mVideoTrack, { kStreamId }); + if (!result_or_error.ok()) { + qDebug() << "Failed to add video track to PeerConnection: " + << result_or_error.error().message(); + } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " m_peer_connection_ AddTrack" + kVideoLabel).toStdString()); + } + +} + WebrtcHanlder::~WebrtcHanlder() { @@ -458,8 +486,12 @@ void MainWindow::itemClicked(QModelIndex index) { if(!mSignalClient->Connected()){ qDebug()<<"请先连接信令服务"; + return; } + mHandler->InitWebrtc(); + this->mLocalVideoTrack = mHandler->VideoTrack(); qDebug()<item(index.row())->text(); + mRemoteName = mModel->item(index.row())->text(); if(mRemoteName == this->mPeerName){ return; diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index af68149..10568d8 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -110,6 +110,9 @@ public: void SetRemoteCandidate(QString); void CreateAnwer(); webrtc::VideoTrackInterface *VideoTrack(); + void AddLocalTrack(); + + protected: ~WebrtcHanlder(); // @@ -142,6 +145,7 @@ private: SignalClient *mClient; QString mRemoteName; webrtc::VideoTrackInterface *mVideoTrack; + webrtc::AudioTrackInterface *mAudioTrack; }; class MainWindow :public QMainWindow