diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index 18e25b6..c3ee93e 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -8,6 +8,9 @@ #include #include +const char kCandidateSdpMidName[] = "sdpMid"; +const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; +const char kCandidateSdpName[] = "candidate"; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -55,6 +58,11 @@ const char kAudioLabel[] = "audio_label"; const char kVideoLabel[] = "video_label"; const char kStreamId[] = "stream_id"; +void WebrtcHanlder::SetRemotePeerName(QString remote) +{ + this->mRemoteName = remote; +} + int WebrtcHanlder::InitWebrtc() { m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( @@ -100,8 +108,7 @@ int WebrtcHanlder::InitWebrtc() qDebug() << "Failed to add audio track to PeerConnection: " << result_or_error.error().message(); } - //rtc::scoped_refptr video_device = - // CapturerTrackSource::Create(); + rtc::scoped_refptr video_device = new rtc::RefCountedObject(); if (video_device) { video_device->startCapturer(); @@ -154,7 +161,6 @@ int WebrtcHanlder::AddTrack() RTC_LOG(LS_ERROR) << "OpenVideoCaptureDevice failed"; } - webrtc::DataChannelInit config; } @@ -165,6 +171,7 @@ void WebrtcHanlder::SetSignalClient(SignalClient * cli) void WebrtcHanlder::CreateOffer() { + qDebug()<<"CreateOffer"; if(nullptr != m_peer_connection_){ m_peer_connection_->CreateOffer(this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); @@ -190,10 +197,18 @@ void WebrtcHanlder::SetRemoteSdp(QString sdp) qDebug()<<"recv" << sdp; auto doc = QJsonDocument::fromJson(sdp.toUtf8()); auto obj = doc.object(); - + auto ssdp = obj["sdp"].toString(); + auto type = obj["type"].toString(); + webrtc::SdpType ktype; webrtc::SdpParseError error; + if(type == "answer"){ + ktype = webrtc::SdpType::kAnswer; + } + if(type == "offer"){ + ktype = webrtc::SdpType::kOffer; + } std::unique_ptr session_description = - webrtc::CreateSessionDescription(webrtc::SdpType::kOffer, sdp.toStdString(), &error); + webrtc::CreateSessionDescription(ktype, ssdp.toStdString(), &error); if (!session_description) { RTC_LOG(WARNING) << "Can't parse received session description message. " @@ -210,8 +225,31 @@ void WebrtcHanlder::SetRemoteSdp(QString sdp) } +void WebrtcHanlder::SetRemoteCandidate(QString scandidate) +{ + qDebug()< candidate( + webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdps, &error)); + + if(nullptr != m_peer_connection_){ + m_peer_connection_->AddIceCandidate(candidate.get()); + } +} + void WebrtcHanlder::CreateAnwer() { + qDebug()<<"create answer"; m_peer_connection_->CreateAnswer( this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); } @@ -225,18 +263,20 @@ void WebrtcHanlder::OnSignalingChange(webrtc::PeerConnectionInterface::Signaling } -void WebrtcHanlder::OnAddTrack(rtc::scoped_refptr receiver, const std::vector > &streams) +void WebrtcHanlder::OnAddTrack(rtc::scoped_refptr receiver, + const std::vector > &streams) { - + qDebug()<<"OnAddTrack"; + RTC_LOG(INFO) << __FUNCTION__ << " " << receiver->id(); } void WebrtcHanlder::OnRemoveTrack(rtc::scoped_refptr receiver) { - + qDebug()<<"OnRemoveTrack"; } void WebrtcHanlder::OnDataChannel(rtc::scoped_refptr channel) { - + qDebug()<<"OnRemoveTrack"; } void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) { @@ -246,14 +286,9 @@ void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceCo void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) { } -const char kCandidateSdpMidName[] = "sdpMid"; -const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; -const char kCandidateSdpName[] = "candidate"; void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidate) { - qDebug()<<"on condidate\r\n"; - QJsonObject addr; addr.insert(kCandidateSdpMidName, candidate->sdp_mid().c_str()); addr.insert(kCandidateSdpMlineIndexName,candidate->sdp_mline_index()); @@ -263,7 +298,8 @@ void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidat return; } addr.insert(kCandidateSdpName,sdp.c_str()); - qDebug()<<"condidate:\r\n"<< QString(QJsonDocument(addr).toJson()); + qDebug()<<"OnIceCandidate:\r\n"<< QString(QJsonDocument(addr).toJson()); + this->mClient->SendICECandidate(mRemoteName,QString(QJsonDocument(addr).toJson())); } void WebrtcHanlder::OnIceConnectionReceivingChange(bool receiving) { @@ -333,22 +369,41 @@ 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); + { + qDebug()<<"2004"; + this->mModel->clear(); + for (auto itr = data.begin();itr != data.end();itr++){ + auto item = new QStandardItem(itr.key()); + item->setEditable(false); + mModel->appendRow(item); + } + break; } - break; - case 2005: - auto sdp = data["sdp"].toString(); - auto remote_name = data["remote_name"].toString(); + case 2005: + { + qDebug()<<"2005"; + auto sdp = data["sdp"].toString(); + auto remote_name = data["remote_name"].toString(); + mHandler->SetRemotePeerName(remote_name); - this->mHandler.get()->SetRemoteSdp(sdp); - mRemoteName = remote_name; - if(!mCalling) - mHandler->CreateAnwer(); - break; + this->mHandler.get()->SetRemoteSdp(sdp); + mRemoteName = remote_name; + if(!mCalling) + mHandler->CreateAnwer(); + break; + } + case 2006: + { + qDebug()<<"2006"; + auto candidate = data["candidate"].toString(); + auto remote_names = data["remote_name"].toString(); + mHandler->SetRemotePeerName(remote_names); + this->mHandler.get()->SetRemoteCandidate(candidate); + mRemoteName = remote_names; + qDebug()<<"recv candidate"<SendSDPOffer(); qDebug()<item(index.row())->text(); mRemoteName = mModel->item(index.row())->text(); + mHandler->SetRemotePeerName(mRemoteName); mCalling = true; } diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index 4ce576c..5d94359 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -35,20 +35,69 @@ namespace Ui { class MainWindow; } QT_END_NAMESPACE +class VideoRenderer : public rtc::VideoSinkInterface { + + public: + VideoRenderer(HWND wnd, + int width, + int height, + webrtc::VideoTrackInterface* track_to_render); + virtual ~VideoRenderer(); + + void Lock() { ::EnterCriticalSection(&buffer_lock_); } + + void Unlock() { ::LeaveCriticalSection(&buffer_lock_); } + + // VideoSinkInterface implementation + void OnFrame(const webrtc::VideoFrame& frame) override; + + const BITMAPINFO& bmi() const { return bmi_; } + const uint8_t* image() const { return image_.get(); } + + protected: + void SetSize(int width, int height); + + enum { + SET_SIZE, + RENDER_FRAME, + }; + + HWND wnd_; + BITMAPINFO bmi_; + std::unique_ptr image_; + CRITICAL_SECTION buffer_lock_; + rtc::scoped_refptr rendered_track_; +}; + +// A little helper class to make sure we always to proper locking and +// unlocking when working with VideoRenderer buffers. +template +class AutoLock { + public: + explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); } + ~AutoLock() { obj_->Unlock(); } + + protected: + T* obj_; +}; + + class WebrtcHanlder :public QObject, public webrtc::PeerConnectionObserver, public webrtc::CreateSessionDescriptionObserver{ Q_OBJECT - signals: void OnOfferSdp(QString); void OnAnswerSdp(QString); public: + void SetRemotePeerName(QString); int InitWebrtc(); int AddTrack(); void SetSignalClient(SignalClient *); void CreateOffer(); void SetRemoteSdp(QString); + void SetRemoteCandidate(QString); + void CreateAnwer(); protected: ~WebrtcHanlder(); @@ -80,6 +129,7 @@ private: rtc::scoped_refptr m_peer_connection_factory_; SignalClient *mClient; + QString mRemoteName; }; diff --git a/client/webrtc_demo/src/signal_client.cpp b/client/webrtc_demo/src/signal_client.cpp index df2695a..814800b 100644 --- a/client/webrtc_demo/src/signal_client.cpp +++ b/client/webrtc_demo/src/signal_client.cpp @@ -91,6 +91,19 @@ void SignalClient::SendSDPOffer(QString peer, QString sdp) qDebug()<