在调用webrtc::PeerConnectionFactoryInterface::CreatePeerConnection之前,需要准备好webrtc::PeerConnectionInterface::RTCConfiguration,下面的代码给出了一个示范,可以看出turn server和stun server的设置方式是不同的。
bool SimplePeerConnection::CreatePeerConnection(const char** turn_urls,
const int no_of_urls,
const char* username,
const char* credential) {
RTC_DCHECK(g_peer_connection_factory.get() != nullptr);
RTC_DCHECK(peer_connection_.get() == nullptr);
local_video_observer_.reset(new VideoObserver());
remote_video_observer_.reset(new VideoObserver());
// Add the turn server.
if (turn_urls != nullptr) {
if (no_of_urls > 0) {
webrtc::PeerConnectionInterface::IceServer turn_server;
for (int i = 0; i < no_of_urls; i++) {
std::string url(turn_urls[i]);
if (url.length() > 0)
turn_server.urls.push_back(turn_urls[i]);
}
std::string user_name(username);
if (user_name.length() > 0)
turn_server.username = username;
std::string password(credential);
if (password.length() > 0)
turn_server.password = credential;
config_.servers.push_back(turn_server);
}
}
// Add the stun server.
webrtc::PeerConnectionInterface::IceServer stun_server;
stun_server.uri = GetPeerConnectionString();
config_.servers.push_back(stun_server);
config_.enable_rtp_data_channel = true;
config_.enable_dtls_srtp = false;
peer_connection_ = g_peer_connection_factory->CreatePeerConnection(
config_, nullptr, nullptr, this);
return peer_connection_.get() != nullptr;
}
本文详细介绍了在调用webrtc::PeerConnectionFactoryInterface::CreatePeerConnection前如何设置webrtc::PeerConnectionInterface::RTCConfiguration,包括TURN和STUN服务器的配置过程。通过示例代码展示了如何正确设置服务器URL、用户名及密码。
1131

被折叠的 条评论
为什么被折叠?



