add local track

master
zcy 2021-12-12 19:58:36 +08:00
parent cc659c89d6
commit 9e1a4a28e6
2 changed files with 70 additions and 34 deletions

View File

@ -64,8 +64,6 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this); ui->setupUi(this);
ui->openGLWidget->show(); ui->openGLWidget->show();
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);
@ -98,10 +96,13 @@ void WebrtcHanlder::SetRemotePeerName(QString remote)
WebrtcHanlder::WebrtcHanlder() WebrtcHanlder::WebrtcHanlder()
{ {
mVideoTrack = nullptr; mVideoTrack = nullptr;
mAudioTrack = nullptr;
m_peer_connection_factory_ = nullptr;
} }
int WebrtcHanlder::InitWebrtc() int WebrtcHanlder::InitWebrtc()
{ {
m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( m_peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
nullptr /* network_thread */, nullptr /* worker_thread */, nullptr /* network_thread */, nullptr /* worker_thread */,
nullptr /* signaling_thread */, nullptr /* default_adm */, nullptr /* signaling_thread */, nullptr /* default_adm */,
@ -125,6 +126,9 @@ int WebrtcHanlder::InitWebrtc()
server.uri = "stun:stun.l.google.com:19302"; // stun服务器 server.uri = "stun:stun.l.google.com:19302"; // stun服务器
config.servers.push_back(server); config.servers.push_back(server);
if(m_peer_connection_ != nullptr){
m_peer_connection_.release();
}
m_peer_connection_ = m_peer_connection_factory_->CreatePeerConnection( m_peer_connection_ = m_peer_connection_factory_->CreatePeerConnection(
config, nullptr, nullptr, this); config, nullptr, nullptr, this);
@ -134,38 +138,7 @@ int WebrtcHanlder::InitWebrtc()
x->show(); x->show();
exit(0); exit(0);
} }
AddLocalTrack();
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()) {
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<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));
qDebug()<<"local track is "<<video_track_->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";
}
} }
@ -269,6 +242,61 @@ webrtc::VideoTrackInterface *WebrtcHanlder::VideoTrack()
return mVideoTrack; return mVideoTrack;
} }
void WebrtcHanlder::AddLocalTrack()
{
if(mAudioTrack == nullptr){
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()) {
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<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));
qDebug()<<"local track is "<<video_track_->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() WebrtcHanlder::~WebrtcHanlder()
{ {
@ -458,8 +486,12 @@ void MainWindow::itemClicked(QModelIndex index)
{ {
if(!mSignalClient->Connected()){ if(!mSignalClient->Connected()){
qDebug()<<"请先连接信令服务"; qDebug()<<"请先连接信令服务";
return;
} }
mHandler->InitWebrtc();
this->mLocalVideoTrack = mHandler->VideoTrack();
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){ if(mRemoteName == this->mPeerName){
return; return;

View File

@ -110,6 +110,9 @@ public:
void SetRemoteCandidate(QString); void SetRemoteCandidate(QString);
void CreateAnwer(); void CreateAnwer();
webrtc::VideoTrackInterface *VideoTrack(); webrtc::VideoTrackInterface *VideoTrack();
void AddLocalTrack();
protected: protected:
~WebrtcHanlder(); ~WebrtcHanlder();
// //
@ -142,6 +145,7 @@ private:
SignalClient *mClient; SignalClient *mClient;
QString mRemoteName; QString mRemoteName;
webrtc::VideoTrackInterface *mVideoTrack; webrtc::VideoTrackInterface *mVideoTrack;
webrtc::AudioTrackInterface *mAudioTrack;
}; };
class MainWindow :public QMainWindow class MainWindow :public QMainWindow