添加详细日志以免后期严格测试

master
18650180552 2021-12-09 00:40:12 +08:00
parent 37f7640556
commit c790786f55
9 changed files with 151 additions and 48 deletions

View File

@ -3,6 +3,7 @@
#include <modules/desktop_capture/desktop_capture_options.h> #include <modules/desktop_capture/desktop_capture_options.h>
#include <QDebug> #include <QDebug>
#include <iostream> #include <iostream>
#include "easylog.h"
MyCapturer::MyCapturer() { MyCapturer::MyCapturer() {

View File

@ -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<std::mutex> 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;
};

View File

@ -0,0 +1,18 @@
#ifndef EASYLOG_H
#define EASYLOG_H
#include <stdio.h>
#include <string>
#include <QDebug>
#include <mutex>
class EasyLog
{
public:
EasyLog(std::string path);
int Log(std::string data);
FILE *mFile;
std::mutex mMux;
};
EasyLog *gLog();
#endif // EASYLOG_H

View File

@ -1,7 +1,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "easylog.h"
#include <QApplication> #include <QApplication>
#include "modules/video_capture/video_capture.h" #include "modules/video_capture/video_capture.h"
#include "video_capturer_test.h" #include "video_capturer_test.h"
#include <QString> #include <QString>
@ -66,6 +65,7 @@ BOOLEAN WINAPI SystemFunction036(PVOID, ULONG);
#pragma comment(lib, "dxgi.lib") #pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "Gdi32.lib") #pragma comment(lib, "Gdi32.lib")
void EnumCapture() void EnumCapture()
{ {
std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info( std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(

View File

@ -10,13 +10,47 @@
#include "video_capture.h" #include "video_capture.h"
#include "video_capturer_test.h" #include "video_capturer_test.h"
#include <iostream> #include <iostream>
#include "easylog.h"
const char kCandidateSdpMidName[] = "sdpMid"; const char kCandidateSdpMidName[] = "sdpMid";
const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex";
const char kCandidateSdpName[] = "candidate"; 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) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
@ -102,8 +136,8 @@ int WebrtcHanlder::InitWebrtc()
qDebug() << "Failed to add audio track to PeerConnection: " qDebug() << "Failed to add audio track to PeerConnection: "
<< result_or_error.error().message(); << result_or_error.error().message();
} }
//rtc::scoped_refptr<CapturerTrackSource> video_device = gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
// CapturerTrackSource::Create(); " m_peer_connection_ AddTrack" + kAudioLabel).toStdString());
rtc::scoped_refptr<MyCapturer> video_device = new rtc::RefCountedObject<MyCapturer>(); rtc::scoped_refptr<MyCapturer> video_device = new rtc::RefCountedObject<MyCapturer>();
if (video_device) { if (video_device) {
video_device->startCapturer(); video_device->startCapturer();
@ -115,47 +149,14 @@ int WebrtcHanlder::InitWebrtc()
qDebug() << "Failed to add video track to PeerConnection: " qDebug() << "Failed to add video track to PeerConnection: "
<< result_or_error.error().message(); << result_or_error.error().message();
} }
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" m_peer_connection_ AddTrack" + kVideoLabel).toStdString());
} else { } else {
qDebug()<< "OpenVideoCaptureDevice failed"; qDebug()<< "OpenVideoCaptureDevice failed";
} }
} }
int WebrtcHanlder::AddTrack()
{
if (!m_peer_connection_->GetSenders().empty()) {
return -1; // Already added tracks.
}
// add audio tracks
rtc::scoped_refptr<webrtc::AudioTrackInterface> 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<CapturerTrackSource> video_device =
// CapturerTrackSource::Create();
rtc::scoped_refptr<MyCapturer> video_device = new rtc::RefCountedObject<MyCapturer>();
if (video_device) {
video_device->startCapturer();
rtc::scoped_refptr<webrtc::VideoTrackInterface> 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) void WebrtcHanlder::SetSignalClient(SignalClient * cli)
{ {
this->mClient = cli; this->mClient = cli;
@ -168,6 +169,8 @@ void WebrtcHanlder::CreateOffer()
m_peer_connection_->CreateOffer(this, m_peer_connection_->CreateOffer(this,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
} }
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" CreateOffer").toStdString());
} }
class DummySetSessionDescriptionObserver class DummySetSessionDescriptionObserver
@ -211,6 +214,9 @@ void WebrtcHanlder::SetRemoteSdp(QString sdp)
DummySetSessionDescriptionObserver::Create(), DummySetSessionDescriptionObserver::Create(),
session_description.release());; 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_){ if(nullptr != m_peer_connection_){
m_peer_connection_->AddIceCandidate(candidate.get()); m_peer_connection_->AddIceCandidate(candidate.get());
} }
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" SetRemoteCandidate&&CreateIceCandidate " + ": " +
ssdp).toStdString());
} }
void WebrtcHanlder::CreateAnwer() void WebrtcHanlder::CreateAnwer()
@ -239,6 +248,8 @@ void WebrtcHanlder::CreateAnwer()
qDebug()<<"create answer"; qDebug()<<"create answer";
m_peer_connection_->CreateAnswer( m_peer_connection_->CreateAnswer(
this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions()); this, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" CreateAnswer ").toStdString());
} }
WebrtcHanlder::~WebrtcHanlder() WebrtcHanlder::~WebrtcHanlder()
@ -253,25 +264,38 @@ void WebrtcHanlder::OnSignalingChange(webrtc::PeerConnectionInterface::Signaling
void WebrtcHanlder::OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface> > &streams) void WebrtcHanlder::OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface> > &streams)
{ {
qDebug()<<"OnAddTrack"<<receiver->id().c_str(); qDebug()<<"OnAddTrack"<<receiver->id().c_str();
if (receiver->track().release()->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) if (receiver->track().release()->kind() ==
webrtc::MediaStreamTrackInterface::kVideoKind)
OnRemoteTrack(receiver->track().release()); OnRemoteTrack(receiver->track().release());
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnAddTrack ").toStdString());
} }
void WebrtcHanlder::OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) void WebrtcHanlder::OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
{ {
qDebug()<<"OnRemoveTrack"; qDebug()<<"OnRemoveTrack";
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnRemoveTrack ").toStdString());
} }
void WebrtcHanlder::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> channel) { void WebrtcHanlder::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> channel) {
qDebug()<<"OnRemoveTrack"; qDebug()<<"OnDataChannel";
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnDataChannel ").toStdString());
} }
void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) { void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) {
qDebug()<<"OnIceConnectionChange"<<new_state; qDebug()<<"OnIceConnectionChange"<<new_state;
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnIceConnectionChange " +
IceConnectionStateName(new_state)).toStdString());
} }
void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) { void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) {
qDebug()<<"OnIceGatheringChange"<<new_state;
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnIceGatheringChange " + IceGatheringStateName(new_state)).toStdString());
} }
void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidate) void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidate)
@ -289,10 +313,14 @@ void WebrtcHanlder::OnIceCandidate(const webrtc::IceCandidateInterface *candidat
addr.insert(kCandidateSdpName,sdp.c_str()); addr.insert(kCandidateSdpName,sdp.c_str());
qDebug()<<"condidate:\r\n"<< QString(QJsonDocument(addr).toJson()); qDebug()<<"condidate:\r\n"<< QString(QJsonDocument(addr).toJson());
this->mClient->SendICECandidate(mRemoteName,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) { 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"){ if(QString(desc->type().c_str()) == "answer"){
OnOfferSdp(QJsonDocument(addr).toJson()); OnOfferSdp(QJsonDocument(addr).toJson());
} }
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
"PeerConnectionObserver::OnSuccess " + QJsonDocument(addr).toJson()).toStdString());
} }
void WebrtcHanlder::OnFailure(webrtc::RTCError error) void WebrtcHanlder::OnFailure(webrtc::RTCError error)
{ {
qDebug()<<error.message(); qDebug()<<error.message();
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
"PeerConnectionObserver::OnSuccess " + error.message()).toStdString());
} }

View File

@ -93,14 +93,12 @@ class WebrtcHanlder :public QObject,
public webrtc::PeerConnectionObserver, public webrtc::PeerConnectionObserver,
public webrtc::CreateSessionDescriptionObserver{ public webrtc::CreateSessionDescriptionObserver{
Q_OBJECT Q_OBJECT
signals: signals:
void OnOfferSdp(QString); void OnOfferSdp(QString);
void OnRemoteTrack(webrtc::MediaStreamTrackInterface*); void OnRemoteTrack(webrtc::MediaStreamTrackInterface*);
public: public:
int InitWebrtc(); int InitWebrtc();
int AddTrack();
void SetSignalClient(SignalClient *); void SetSignalClient(SignalClient *);
void CreateOffer(); void CreateOffer();
void CreateAnswer(); void CreateAnswer();

View File

@ -2,6 +2,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <windows.h> #include <windows.h>
#include <Lmcons.h> #include <Lmcons.h>
#include "easylog.h"
SignalClient::SignalClient(const QUrl &url, bool debug, QObject *parent): SignalClient::SignalClient(const QUrl &url, bool debug, QObject *parent):
QObject(parent), QObject(parent),
@ -39,6 +40,9 @@ void SignalClient::SendLogin()
obj.insert("data",data); obj.insert("data",data);
m_peer_name = QString::fromWCharArray(username); m_peer_name = QString::fromWCharArray(username);
m_webSocket.sendTextMessage(QJsonDocument(obj).toJson()); 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 #define REQ_INROOM 1001
@ -52,6 +56,8 @@ void SignalClient::SendInRoom(QString room)
obj.insert("type",REQ_INROOM); obj.insert("type",REQ_INROOM);
obj.insert("data",data); obj.insert("data",data);
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); 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 #define REQ_INROOM 1004
@ -64,6 +70,8 @@ void SignalClient::SendListRoom(QString room)
obj.insert("type",REQ_INROOM); obj.insert("type",REQ_INROOM);
obj.insert("data",data); obj.insert("data",data);
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); 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() bool SignalClient::Connected()
@ -89,7 +97,8 @@ void SignalClient::SendSDPOffer(QString peer, QString sdp)
qDebug()<<QString(QJsonDocument(obj).toJson()); qDebug()<<QString(QJsonDocument(obj).toJson());
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson()));
qDebug()<<QString(QJsonDocument(obj).toJson()); gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" +
QString(QJsonDocument(obj).toJson())).toStdString());
} }
#define REQ_CANDIDATE 1006 #define REQ_CANDIDATE 1006
void SignalClient::SendICECandidate(QString peer, QString candidate) void SignalClient::SendICECandidate(QString peer, QString candidate)
@ -103,6 +112,8 @@ void SignalClient::SendICECandidate(QString peer, QString candidate)
obj.insert("data",data); obj.insert("data",data);
qDebug()<<QString(QJsonDocument(obj).toJson()); qDebug()<<QString(QJsonDocument(obj).toJson());
m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson()));
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" send:\r\n" +
QString(QJsonDocument(obj).toJson())).toStdString());
} }
QString SignalClient::PeerName() QString SignalClient::PeerName()
@ -118,6 +129,8 @@ void SignalClient::onConnected()
m_connected = true; m_connected = true;
m_timer.stop(); m_timer.stop();
connected(); connected();
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +" +"
" WebSocket connected").toStdString());
} }
void SignalClient::onUnConnected() void SignalClient::onUnConnected()
@ -127,6 +140,9 @@ void SignalClient::onUnConnected()
m_timer.setSingleShot(false); m_timer.setSingleShot(false);
m_webSocket.open(QUrl(m_url)); m_webSocket.open(QUrl(m_url));
closed(); closed();
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" WebSocket unconnected").toStdString());
} }
void SignalClient::onTextMessageReceived(QString message) void SignalClient::onTextMessageReceived(QString message)
@ -135,11 +151,16 @@ void SignalClient::onTextMessageReceived(QString message)
auto obj = QJsonDocument::fromJson(message.toUtf8()).object(); auto obj = QJsonDocument::fromJson(message.toUtf8()).object();
qDebug()<<obj["type"]; qDebug()<<obj["type"];
response(obj["type"].toInt(),obj["data"].toObject()); response(obj["type"].toInt(),obj["data"].toObject());
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + " recv:\r\n" +
QString(QJsonDocument(obj).toJson())).toStdString());
} }
void SignalClient::onSocketError(QAbstractSocket::SocketError error) void SignalClient::onSocketError(QAbstractSocket::SocketError error)
{ {
qDebug()<<error; qDebug()<<error;
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" WebSocket unconnected").toStdString());
} }
void SignalClient::onReconenectTimeout() void SignalClient::onReconenectTimeout()

View File

@ -2,7 +2,7 @@
#define VIDEO_CAPTURE_H #define VIDEO_CAPTURE_H
// vcm_capturer_test.h // vcm_capturer_test.h
#include "easylog.h"
#include <memory> #include <memory>
#include <QObject> #include <QObject>

View File

@ -23,6 +23,7 @@ LIBS += -L$$PWD/third/lib libwebrtc.lib ole32.lib oleaut32.lib strmiids.lib yuv
SOURCES += \ SOURCES += \
src/MyCapturer.cpp \ src/MyCapturer.cpp \
src/cplaywidget.cpp \ src/cplaywidget.cpp \
src/easylog.cpp \
src/main.cpp \ src/main.cpp \
src/mainwindow.cpp \ src/mainwindow.cpp \
src/signal_client.cpp \ src/signal_client.cpp \
@ -32,6 +33,7 @@ SOURCES += \
HEADERS += \ HEADERS += \
src/MyCapturer.h \ src/MyCapturer.h \
src/cplaywidget.h \ src/cplaywidget.h \
src/easylog.h \
src/mainwindow.h \ src/mainwindow.h \
src/signal_client.h \ src/signal_client.h \
src/video_capture.h \ src/video_capture.h \