no message

master
zcy 2021-12-11 23:27:41 +08:00
parent 8466492420
commit cc659c89d6
5 changed files with 97 additions and 25 deletions

View File

@ -15,6 +15,7 @@ void MyCapturer::startCapturer() {
capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options); capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options);
capturer_->Start(this); capturer_->Start(this);
CaptureFrame(); CaptureFrame();
} }
webrtc::MediaSourceInterface::SourceState MyCapturer::state() const { webrtc::MediaSourceInterface::SourceState MyCapturer::state() const {

View File

@ -99,9 +99,9 @@ void InitCustomMetaType(){
qRegisterMetaType<uint8_t*>("uint8_t*"); qRegisterMetaType<uint8_t*>("uint8_t*");
qRegisterMetaType<uint64_t>("uint64_t"); qRegisterMetaType<uint64_t>("uint64_t");
qRegisterMetaType<uint32_t>("uint32_t"); qRegisterMetaType<uint32_t>("uint32_t");
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
InitCustomMetaType(); InitCustomMetaType();
@ -118,11 +118,6 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow w; MainWindow w;
// QObject::connect((VcmCapturerTest*)capturer.get(),SIGNAL(UpdateFrame(rtc::scoped_refptr<webrtc::I420BufferInterface>&)),&w,
// SLOT(OnUpdateFrame( rtc::scoped_refptr<webrtc::I420BufferInterface>&)),Qt::ConnectionType::QueuedConnection);
// QObject::connect((VcmCapturerTest*)capturer.get(),SIGNAL(UpdateFrame1(uint8_t*)),&w,
// SLOT(OnUpdateFrame1( uint8_t *)),Qt::ConnectionType::QueuedConnection);
w.show(); w.show();
return a.exec(); return a.exec();
} }

View File

@ -58,15 +58,18 @@ MainWindow::MainWindow(QWidget *parent)
,mSignalClient(nullptr) ,mSignalClient(nullptr)
,mModel(nullptr) ,mModel(nullptr)
,mCalling(false) ,mCalling(false)
,mRemoteStream(nullptr) ,mRemoteVideoTrack(nullptr)
,mLocalVideoTrack(nullptr)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->openGLWidget->show(); ui->openGLWidget->show();
mHandler->InitWebrtc(); mHandler->InitWebrtc();
this->mLocalVideoTrack = mHandler->VideoTrack();
TCHAR username[UNLEN + 1]; TCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1; DWORD size = UNLEN + 1;
GetUserName((TCHAR*)username, &size); GetUserName((TCHAR*)username, &size);
this->mPeerName = QString::fromWCharArray(username);
mModel = new QStandardItemModel(this); mModel = new QStandardItemModel(this);
ui->treeView->setModel(mModel); ui->treeView->setModel(mModel);
connect(ui->treeView,SIGNAL(doubleClicked(QModelIndex)), connect(ui->treeView,SIGNAL(doubleClicked(QModelIndex)),
@ -92,6 +95,11 @@ void WebrtcHanlder::SetRemotePeerName(QString remote)
this->mRemoteName = remote; this->mRemoteName = remote;
} }
WebrtcHanlder::WebrtcHanlder()
{
mVideoTrack = nullptr;
}
int WebrtcHanlder::InitWebrtc() int WebrtcHanlder::InitWebrtc()
{ {
m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
@ -137,6 +145,7 @@ 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();
} }
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" m_peer_connection_ AddTrack" + kAudioLabel).toStdString()); " 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>();
@ -146,6 +155,8 @@ int WebrtcHanlder::InitWebrtc()
m_peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device)); m_peer_connection_factory_->CreateVideoTrack(kVideoLabel, video_device));
qDebug()<<"local track is "<<video_track_->id().c_str(); qDebug()<<"local track is "<<video_track_->id().c_str();
result_or_error = m_peer_connection_->AddTrack(video_track_, { kStreamId }); result_or_error = m_peer_connection_->AddTrack(video_track_, { kStreamId });
mVideoTrack = video_track_.get();
video_track_->AddRef();
if (!result_or_error.ok()) { if (!result_or_error.ok()) {
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();
@ -253,6 +264,11 @@ void WebrtcHanlder::CreateAnwer()
" CreateAnswer ").toStdString()); " CreateAnswer ").toStdString());
} }
webrtc::VideoTrackInterface *WebrtcHanlder::VideoTrack()
{
return mVideoTrack;
}
WebrtcHanlder::~WebrtcHanlder() WebrtcHanlder::~WebrtcHanlder()
{ {
@ -291,6 +307,15 @@ void WebrtcHanlder::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceCo
gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) + gLog()->Log((QString::asprintf("%s %d",__FILE__,__LINE__) +
" OnIceConnectionChange " + " OnIceConnectionChange " +
IceConnectionStateName(new_state)).toStdString()); 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) { void WebrtcHanlder::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) {
@ -434,13 +459,14 @@ void MainWindow::itemClicked(QModelIndex index)
if(!mSignalClient->Connected()){ if(!mSignalClient->Connected()){
qDebug()<<"请先连接信令服务"; qDebug()<<"请先连接信令服务";
} }
mHandler->CreateOffer();
//mSignalClient->SendSDPOffer();
qDebug()<<mModel->item(index.row())->text(); qDebug()<<mModel->item(index.row())->text();
mRemoteName = mModel->item(index.row())->text(); mRemoteName = mModel->item(index.row())->text();
if(mRemoteName == this->mPeerName){
return;
}
mHandler->CreateOffer();
mHandler->SetRemotePeerName(mRemoteName); mHandler->SetRemotePeerName(mRemoteName);
mCalling = true; mCalling = true;
} }
void MainWindow::on_local_sdp(QString sdp) 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) void MainWindow::on_track_add(webrtc::MediaStreamTrackInterface *data)
{ {
qDebug()<<"on_track_add"<<data->kind().c_str(); qDebug()<<"on_track_add"<<data->kind().c_str();
mRemoteStream = reinterpret_cast<webrtc::MediaStreamTrackInterface*>(data); mRemoteVideoTrack = reinterpret_cast<webrtc::VideoTrackInterface*>(data);
if (mRemoteStream->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) { if (mRemoteVideoTrack->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
auto* video_track = static_cast<webrtc::VideoTrackInterface*>(mRemoteStream); mRemoteVideoTrack = static_cast<webrtc::VideoTrackInterface*>(mRemoteVideoTrack);
qDebug()<<"remote trackid is "<<video_track->id().c_str(); qDebug()<<"remote trackid is "<<mRemoteVideoTrack->id().c_str();
remote_renderer_.reset(new VideoRenderer( 1, 1, video_track)); remote_renderer_.reset(new VideoRenderer( 1, 1, mRemoteVideoTrack));
connect((VideoRenderer*)(remote_renderer_.get()), connect((VideoRenderer*)(remote_renderer_.get()),
SIGNAL(UpdateFrame(rtc::scoped_refptr<webrtc::I420BufferInterface>&)), SIGNAL(UpdateFrame(rtc::scoped_refptr<webrtc::I420BufferInterface>&)),
@ -620,13 +646,29 @@ void VideoRenderer::SetSize(int width, int height)
void MainWindow::on_pushButton_3_clicked() void MainWindow::on_pushButton_3_clicked()
{ {
if(mRemoteStream != nullptr){ if(mRemoteVideoTrack != nullptr){
mRemoteStream->set_enabled(false); mRemoteVideoTrack->set_enabled(false);
} }
} }
void MainWindow::on_pushButton_4_clicked() 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);
}
} }

View File

@ -96,8 +96,11 @@ class WebrtcHanlder :public QObject,
signals: signals:
void OnOfferSdp(QString); void OnOfferSdp(QString);
void OnRemoteTrack(webrtc::MediaStreamTrackInterface*); void OnRemoteTrack(webrtc::MediaStreamTrackInterface*);
void OnConectionDisconnected();
void OnConectionComplete();
public: public:
WebrtcHanlder();
int InitWebrtc(); int InitWebrtc();
void SetSignalClient(SignalClient *); void SetSignalClient(SignalClient *);
void CreateOffer(); void CreateOffer();
@ -106,6 +109,7 @@ public:
void SetRemoteSdp(QString); void SetRemoteSdp(QString);
void SetRemoteCandidate(QString); void SetRemoteCandidate(QString);
void CreateAnwer(); void CreateAnwer();
webrtc::VideoTrackInterface *VideoTrack();
protected: protected:
~WebrtcHanlder(); ~WebrtcHanlder();
// //
@ -137,6 +141,7 @@ private:
m_peer_connection_factory_; m_peer_connection_factory_;
SignalClient *mClient; SignalClient *mClient;
QString mRemoteName; QString mRemoteName;
webrtc::VideoTrackInterface *mVideoTrack;
}; };
class MainWindow :public QMainWindow class MainWindow :public QMainWindow
@ -159,6 +164,12 @@ private slots:
void on_notify_size(uint32_t,uint32_t); void on_notify_size(uint32_t,uint32_t);
void on_pushButton_3_clicked(); void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
rtc::scoped_refptr<WebrtcHanlder> mHandler; rtc::scoped_refptr<WebrtcHanlder> mHandler;
@ -169,6 +180,8 @@ private:
bool mCalling; bool mCalling;
std::unique_ptr<VideoRenderer> local_renderer_; std::unique_ptr<VideoRenderer> local_renderer_;
std::unique_ptr<VideoRenderer> remote_renderer_; std::unique_ptr<VideoRenderer> remote_renderer_;
webrtc::MediaStreamTrackInterface* mRemoteStream; webrtc::VideoTrackInterface* mRemoteVideoTrack;
webrtc::VideoTrackInterface* mLocalVideoTrack;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -73,7 +73,7 @@
<item> <item>
<widget class="QLineEdit" name="lineEdit_2"> <widget class="QLineEdit" name="lineEdit_2">
<property name="text"> <property name="text">
<string>ws://192.168.5.192:9555/ws</string> <string>ws://192.168.5.73:9555/ws</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -137,7 +137,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_4" stretch="0,0,0,8"> <layout class="QHBoxLayout" name="horizontalLayout_4" stretch="0,0,0,0,0,8">
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="horizontalSpacer_2">
<property name="orientation"> <property name="orientation">
@ -154,14 +154,28 @@
<item> <item>
<widget class="QPushButton" name="pushButton_3"> <widget class="QPushButton" name="pushButton_3">
<property name="text"> <property name="text">
<string>暂停对方视频流</string> <string>暂停对方视频接收</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>恢复对方视频接收</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="pushButton_4"> <widget class="QPushButton" name="pushButton_4">
<property name="text"> <property name="text">
<string>暂停本地视频流</string> <string>暂停本地视频流采集</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_6">
<property name="text">
<string>恢复本地视频采集</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -181,7 +195,7 @@
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0"> <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="0,0,0">
<item> <item>
<widget class="QLabel" name="label_6"> <widget class="QLabel" name="label_6">
<property name="text"> <property name="text">
@ -196,6 +210,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>