diff --git a/client/webrtc_demo/src/MyCapturer.cpp b/client/webrtc_demo/src/MyCapturer.cpp index 8e30f44..e362a45 100644 --- a/client/webrtc_demo/src/MyCapturer.cpp +++ b/client/webrtc_demo/src/MyCapturer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "easylog.h" MyCapturer::MyCapturer() { diff --git a/client/webrtc_demo/src/easylog.cpp b/client/webrtc_demo/src/easylog.cpp new file mode 100644 index 0000000..e37d30c --- /dev/null +++ b/client/webrtc_demo/src/easylog.cpp @@ -0,0 +1,31 @@ +#include "easylog.h" + +EasyLog *gLog(){ + static EasyLog *gsLog ; + if (gsLog == nullptr) + gsLog = new EasyLog("d://peerconection.txt"); + return gsLog; +} + +EasyLog::EasyLog(std::string path): + mFile(nullptr) +{ + mFile = fopen(path.c_str(),"w+"); + if(mFile == nullptr){ + qDebug()<<"easylog"; + } +} + +int EasyLog::Log(std::string data) +{ + std::unique_lock mutex(mMux); + if((mFile == nullptr)){ + return -1; + } + int ret = fwrite(data.c_str(),data.size(),1,mFile); + fwrite("\r\n",2,1,mFile); + + fflush(mFile); + return ret; +}; + diff --git a/client/webrtc_demo/src/easylog.h b/client/webrtc_demo/src/easylog.h new file mode 100644 index 0000000..49ccccb --- /dev/null +++ b/client/webrtc_demo/src/easylog.h @@ -0,0 +1,18 @@ +#ifndef EASYLOG_H +#define EASYLOG_H +#include +#include +#include +#include +class EasyLog +{ +public: + EasyLog(std::string path); + int Log(std::string data); + FILE *mFile; + std::mutex mMux; +}; + +EasyLog *gLog(); + +#endif // EASYLOG_H diff --git a/client/webrtc_demo/src/main.cpp b/client/webrtc_demo/src/main.cpp index 7684990..786bbbd 100644 --- a/client/webrtc_demo/src/main.cpp +++ b/client/webrtc_demo/src/main.cpp @@ -1,7 +1,6 @@ #include "mainwindow.h" - +#include "easylog.h" #include - #include "modules/video_capture/video_capture.h" #include "video_capturer_test.h" #include @@ -66,6 +65,7 @@ BOOLEAN WINAPI SystemFunction036(PVOID, ULONG); #pragma comment(lib, "dxgi.lib") #pragma comment(lib, "Gdi32.lib") + void EnumCapture() { std::unique_ptr info( diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index 297b077..025daed 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -10,13 +10,47 @@ #include "video_capture.h" #include "video_capturer_test.h" #include - +#include "easylog.h" const char kCandidateSdpMidName[] = "sdpMid"; const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; const char kCandidateSdpName[] = "candidate"; +QString IceGatheringStateName(webrtc::PeerConnectionInterface::IceGatheringState w){ + switch (w) { + case webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringNew: + return "kIceGatheringNew"; + case webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringGathering: + return "kIceGatheringGathering"; + case webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringComplete: + return "kIceGatheringComplete"; + } + +} + +QString IceConnectionStateName(webrtc::PeerConnectionInterface::IceConnectionState w){ + switch(w){ + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionNew: + return "kIceConnectionNew"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionChecking: + return "kIceConnectionChecking"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionConnected: + return "kIceConnectionConnected"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionCompleted: + return "kIceConnectionCompleted"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionFailed: + return "kIceConnectionFailed"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionDisconnected: + return "kIceConnectionDisconnected"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionClosed: + return "kIceConnectionClosed"; + case webrtc::PeerConnectionInterface::IceConnectionState::kIceConnectionMax: + return "kIceConnectionMax"; + } +} + + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) @@ -102,8 +136,8 @@ int WebrtcHanlder::InitWebrtc() qDebug() << "Failed to add audio track to PeerConnection: " << result_or_error.error().message(); } - //rtc::scoped_refptr video_device = - // CapturerTrackSource::Create(); + 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(); @@ -115,47 +149,14 @@ int WebrtcHanlder::InitWebrtc() 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"; } } -int WebrtcHanlder::AddTrack() -{ - if (!m_peer_connection_->GetSenders().empty()) { - return -1; // Already added tracks. - } - // add audio tracks - 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()) { - RTC_LOG(LS_ERROR) << "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(); - rtc::scoped_refptr video_track_( - m_peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device)); - - result_or_error = m_peer_connection_->AddTrack(video_track_, { kStreamId }); - if (!result_or_error.ok()) { - RTC_LOG(LS_ERROR) << "Failed to add video track to PeerConnection: " - << result_or_error.error().message(); - } - } else { - RTC_LOG(LS_ERROR) << "OpenVideoCaptureDevice failed"; - } - webrtc::DataChannelInit config; -} - void WebrtcHanlder::SetSignalClient(SignalClient * cli) { this->mClient = cli; @@ -168,6 +169,8 @@ void WebrtcHanlder::CreateOffer() m_peer_connection_->CreateOffer(this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " CreateOffer").toStdString()); } class DummySetSessionDescriptionObserver @@ -211,6 +214,9 @@ void WebrtcHanlder::SetRemoteSdp(QString sdp) DummySetSessionDescriptionObserver::Create(), session_description.release());; } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " SetRemoteSdp " + type + ": " + + ssdp).toStdString()); } @@ -232,6 +238,9 @@ void WebrtcHanlder::SetRemoteCandidate(QString sdp) if(nullptr != m_peer_connection_){ m_peer_connection_->AddIceCandidate(candidate.get()); } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " SetRemoteCandidate&&CreateIceCandidate " + ": " + + ssdp).toStdString()); } void WebrtcHanlder::CreateAnwer() @@ -239,6 +248,8 @@ void WebrtcHanlder::CreateAnwer() qDebug()<<"create answer"; m_peer_connection_->CreateAnswer( this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " CreateAnswer ").toStdString()); } WebrtcHanlder::~WebrtcHanlder() @@ -253,25 +264,38 @@ void WebrtcHanlder::OnSignalingChange(webrtc::PeerConnectionInterface::Signaling void WebrtcHanlder::OnAddTrack(rtc::scoped_refptr receiver, const std::vector > &streams) { qDebug()<<"OnAddTrack"<id().c_str(); - if (receiver->track().release()->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) + if (receiver->track().release()->kind() == + webrtc::MediaStreamTrackInterface::kVideoKind) OnRemoteTrack(receiver->track().release()); + + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnAddTrack ").toStdString()); } void WebrtcHanlder::OnRemoveTrack(rtc::scoped_refptr receiver) { qDebug()<<"OnRemoveTrack"; + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnRemoveTrack ").toStdString()); } void WebrtcHanlder::OnDataChannel(rtc::scoped_refptr channel) { - qDebug()<<"OnRemoveTrack"; + qDebug()<<"OnDataChannel"; + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnDataChannel ").toStdString()); } void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) { qDebug()<<"OnIceConnectionChange"<Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnIceConnectionChange " + + IceConnectionStateName(new_state)).toStdString()); } void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) { - + qDebug()<<"OnIceGatheringChange"<Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnIceGatheringChange " + IceGatheringStateName(new_state)).toStdString()); } void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidate) @@ -289,10 +313,14 @@ void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidat addr.insert(kCandidateSdpName,sdp.c_str()); qDebug()<<"condidate:\r\n"<< QString(QJsonDocument(addr).toJson()); this->mClient->SendICECandidate(mRemoteName,QString(QJsonDocument(addr).toJson())); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnIceCandidate " + QString(QJsonDocument(addr).toJson())).toStdString()); } void WebrtcHanlder::OnIceConnectionReceivingChange(bool receiving) { - + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " OnIceConnectionReceivingChange " +\ + QString::asprintf("%d",receiving)).toStdString()); } @@ -316,11 +344,15 @@ void WebrtcHanlder::OnSuccess(webrtc::SessionDescriptionInterface *desc) if(QString(desc->type().c_str()) == "answer"){ OnOfferSdp(QJsonDocument(addr).toJson()); } + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + "PeerConnectionObserver::OnSuccess " + QJsonDocument(addr).toJson()).toStdString()); } void WebrtcHanlder::OnFailure(webrtc::RTCError error) { qDebug()<Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + "PeerConnectionObserver::OnSuccess " + error.message()).toStdString()); } diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index 1869a73..01c3172 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -93,14 +93,12 @@ class WebrtcHanlder :public QObject, public webrtc::PeerConnectionObserver, public webrtc::CreateSessionDescriptionObserver{ Q_OBJECT - signals: void OnOfferSdp(QString); void OnRemoteTrack(webrtc::MediaStreamTrackInterface*); public: int InitWebrtc(); - int AddTrack(); void SetSignalClient(SignalClient *); void CreateOffer(); void CreateAnswer(); diff --git a/client/webrtc_demo/src/signal_client.cpp b/client/webrtc_demo/src/signal_client.cpp index 814800b..fac3d65 100644 --- a/client/webrtc_demo/src/signal_client.cpp +++ b/client/webrtc_demo/src/signal_client.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "easylog.h" SignalClient::SignalClient(const QUrl &url, bool debug, QObject *parent): QObject(parent), @@ -39,6 +40,9 @@ void SignalClient::SendLogin() obj.insert("data",data); m_peer_name = QString::fromWCharArray(username); m_webSocket.sendTextMessage(QJsonDocument(obj).toJson()); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); + } #define REQ_INROOM 1001 @@ -52,6 +56,8 @@ void SignalClient::SendInRoom(QString room) obj.insert("type",REQ_INROOM); obj.insert("data",data); m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); } #define REQ_INROOM 1004 @@ -64,6 +70,8 @@ void SignalClient::SendListRoom(QString room) obj.insert("type",REQ_INROOM); obj.insert("data",data); m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); } bool SignalClient::Connected() @@ -89,7 +97,8 @@ void SignalClient::SendSDPOffer(QString peer, QString sdp) qDebug()<Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); } #define REQ_CANDIDATE 1006 void SignalClient::SendICECandidate(QString peer, QString candidate) @@ -103,6 +112,8 @@ void SignalClient::SendICECandidate(QString peer, QString candidate) obj.insert("data",data); qDebug()<Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); } QString SignalClient::PeerName() @@ -118,6 +129,8 @@ void SignalClient::onConnected() m_connected = true; m_timer.stop(); connected(); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" +" + " WebSocket connected").toStdString()); } void SignalClient::onUnConnected() @@ -127,6 +140,9 @@ void SignalClient::onUnConnected() m_timer.setSingleShot(false); m_webSocket.open(QUrl(m_url)); closed(); + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " WebSocket unconnected").toStdString()); + } void SignalClient::onTextMessageReceived(QString message) @@ -135,11 +151,16 @@ void SignalClient::onTextMessageReceived(QString message) auto obj = QJsonDocument::fromJson(message.toUtf8()).object(); qDebug()<Log((QString::asprintf("%s %d",__FILE__,__LINE__) + " recv:\r\n" + + QString(QJsonDocument(obj).toJson())).toStdString()); } void SignalClient::onSocketError(QAbstractSocket::SocketError error) { qDebug()<Log((QString::asprintf("%s %d",__FILE__,__LINE__) + + " WebSocket unconnected").toStdString()); + } void SignalClient::onReconenectTimeout() diff --git a/client/webrtc_demo/src/video_capture.h b/client/webrtc_demo/src/video_capture.h index 58c4418..9016a53 100644 --- a/client/webrtc_demo/src/video_capture.h +++ b/client/webrtc_demo/src/video_capture.h @@ -2,7 +2,7 @@ #define VIDEO_CAPTURE_H // vcm_capturer_test.h - +#include "easylog.h" #include #include diff --git a/client/webrtc_demo/webrtc_demo.pro b/client/webrtc_demo/webrtc_demo.pro index 7b3a0fc..1697823 100644 --- a/client/webrtc_demo/webrtc_demo.pro +++ b/client/webrtc_demo/webrtc_demo.pro @@ -23,6 +23,7 @@ LIBS += -L$$PWD/third/lib libwebrtc.lib ole32.lib oleaut32.lib strmiids.lib yuv SOURCES += \ src/MyCapturer.cpp \ src/cplaywidget.cpp \ + src/easylog.cpp \ src/main.cpp \ src/mainwindow.cpp \ src/signal_client.cpp \ @@ -32,6 +33,7 @@ SOURCES += \ HEADERS += \ src/MyCapturer.h \ src/cplaywidget.h \ + src/easylog.h \ src/mainwindow.h \ src/signal_client.h \ src/video_capture.h \