webrtc 代码学习(四)创建PC

本文简要介绍WebRTC中创建PeerConnection的过程,包括PeerConnectionFactory的CreatePeerConnection方法,PeerConnectionProxy的原理,以及PeerConnection的Initialize操作。通过分析源代码,帮助理解这一复杂流程。
摘要由CSDN通过智能技术生成

创建PC
作者:LanPZzzz
本章我们简单讲述下PC 创建,PC 是我最头疼的一个类(┳_┳)…,太庞大了,可以说是整个webrtc中最复杂的,我感觉没有之一了,.h就1000行,.cc 就更不敢想象,你就不能分一下啊,能死啊… 我们还是要分析下PC 的内容,工程巨大啊

1. PC 创建
bool Conductor::CreatePeerConnection(bool dtls) {
  RTC_DCHECK(peer_connection_factory_);
  RTC_DCHECK(!peer_connection_);

  webrtc::PeerConnectionInterface::RTCConfiguration config;
  1. 使用kUnifiedPlan,因为kUnifiedPlan 是为了的趋势,PlanB和PlanA 必将被淘汰的
  config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
  2. dtls = false
  config.enable_dtls_srtp = dtls;
  webrtc::PeerConnectionInterface::IceServer server;
  3. server.uri = "stun:43.240.127.199:5000",这个应该是google 的stun 服务器,当然也可以自己搭建
  server.uri = GetPeerConnectionString();
  config.servers.push_back(server);

  4. 创建PC
  peer_connection_ = peer_connection_factory_->CreatePeerConnection(
      config, nullptr, nullptr, this);
  return peer_connection_ != nullptr;
}
2. PeerConnectionFactory::CreatePeerConnection(pc\peerconnectionfactory.cc 363行)
rtc::scoped_refptr<PeerConnectionInterface>
PeerConnectionFactory::CreatePeerConnection(
    const PeerConnectionInterface::RTCConfiguration& configuration,
    PeerConnectionDependencies dependencies) {
  RTC_DCHECK(signaling_thread_->IsCurrent());

  1. 我们在创出PC 时cert_generator = nullptr 和 allocator = nullptr,都是要重新创建的
  // Set internal defaults if optional dependencies are not set.
  if (!dependencies.cert_generator) {
    dependencies.cert_generator =
        absl::make_unique<rtc::RTCCertificateGenerator>(signaling_thread_,
                                                        network_thread_);
  }

  if (!dependencies.allocator) {
    dependencies.allocator.reset(new cricket::BasicPortAllocator(
        default_network_manager_.get(), default_socket_factory_.get(),
        configuration.turn_customizer));
  }

  network_thread_->Invoke<void>(
      RTC_FROM_HERE,
      rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask,
                dependencies.allocator.get(), options_.network_ignore_mask));

  2. event_log 创建
  std::unique_ptr<RtcEventLog> event_log =
      worker_thread_->Invoke<std::unique_ptr<RtcEventLog>>(
          RTC_FROM_HERE,
          rtc::Bind(&PeerConnectionFactory::CreateRtcEventLog_w, this));

  3. Call 创建,Call 感觉应该是个很重要的对象,**具体干什么,要在后续学习了
  std::unique_ptr<Call> call = worker_thread_->Invoke<std::unique_ptr<Call>>(
      RTC_FROM_HERE,
      rtc::Bind(&PeerConnectionFactory::CreateCall_w, this, event_log.get()));

  4. new PC
  rtc::scoped_refptr<PeerConnection> pc(
      new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log),
                                                std::move(call)));
  5. PC Initialize
  ActionsBeforeInitializeF
### 回答1: WebRTC是一种用于实现浏览器之间实时通信的开源项目。下面是一个简单的WebRTC C语言示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <webrtc/api/peerconnectioninterface.h> void CreateOfferCallback(webrtc::SessionDescriptionInterface* desc) { std::string sdp; desc->ToString(&sdp); printf("SDP offer:\n%s\n", sdp.c_str()); } int main() { // 初始化PeerConnection库 webrtc::PeerConnectionFactoryInterface* peer_connection_factory = webrtc::CreatePeerConnectionFactory(); // 创建一个PeerConnection webrtc::PeerConnectionInterface::RTCConfiguration config; webrtc::PeerConnectionInterface* peer_connection = peer_connection_factory->CreatePeerConnection(config, nullptr); if (!peer_connection) { printf("Failed to create PeerConnection\n"); return -1; } // 配置PeerConnection的本地媒体 webrtc::MediaConstraintsInterface* constraints = new webrtc::MediaConstraintsInterface(); peer_connection->AddStream(constraints); // 创建offer并设置offer回调 peer_connection->CreateOffer(new webrtc::CreateOfferCallback, constraints); // 主循环 while (1) { // 处理PeerConnection的事件 peer_connection->ProcessMessages(); } // 释放资源 delete constraints; delete peer_connection; delete peer_connection_factory; return 0; } ``` 这段示例代码展示了如何使用WebRTC的C接口创建一个简单的PeerConnection,并创建Offer和接收Answer来建立连接。在代码中,我们首先初始化PeerConnectionFactory,然后创建一个PeerConnection,并设置本地媒体。接下来创建Offer并设置Offer回调,通过回调函数可以获得生成的SDP(会话描述协议)Offer。最后在主循环中处理PeerConnection的事件,直到程序结束。注意,这段代码仅仅是一个示例,实际应用中可能需要处理更多的细节和错误处理。 ### 回答2: WebRTC是一种用于实时音视频通信的开源技术,其C语言示例代码可以用于实现基于C语言的音视频通信应用。下面是一个简单的WebRTC C示例代码: ```c // 包含WebRTC相关的头文件 #include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <string.h> // WebRTC的初始化函数 int webrtc_init() { // 初始化WebRTC库 // ... return 0; } // 创建PeerConnection(对等连接) int create_peer_connection() { // 创建PeerConnection对象 // ... return 0; } // 发送音视频数据 int send_media_data(uint8_t* data, size_t size) { // 将音视频数据通过WebRTC发送出去 // ... return 0; } // 接收音视频数据 int receive_media_data(uint8_t* data, size_t size) { // 从WebRTC接收音视频数据 // ... return 0; } // 主函数 int main() { // 初始化WebRTC if (webrtc_init() != 0) { printf("WebRTC初始化失败\n"); return 1; } // 创建PeerConnection if (create_peer_connection() != 0) { printf("创建PeerConnection失败\n"); return 1; } // 发送和接收音视频数据 uint8_t data[] = {1, 2, 3, 4, 5}; if (send_media_data(data, sizeof(data)) != 0) { printf("发送音视频数据失败\n"); return 1; } uint8_t received_data[10]; if (receive_media_data(received_data, sizeof(received_data)) != 0) { printf("接收音视频数据失败\n"); return 1; } // 打印接收到的音视频数据 printf("接收到的音视频数据: "); for (size_t i = 0; i < sizeof(received_data); ++i) { printf("%d ", received_data[i]); } printf("\n"); return 0; } ``` 请注意,以上示例代码只是简单演示了WebRTC在C语言中的使用,实际应用中还需要根据具体需求进行更多的配置和处理。也需要使用WebRTC的库和函数来进行实际的音视频传输、信令等操作。 ### 回答3: WebRTC(Web实时通信)是一个开放源代码的项目,用于实时音视频通信和数据传输。它提供了一组API和一些示例代码,使开发者可以在网页中实现音视频通信功能。 下面是一个简单的WebRTC C语言示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <webrtc.h> // 声明回调函数 void onReceiveData(const char* data, int length) { printf("Received data: %s\n", data); } int main() { // 初始化WebRTC webrtc_init(); // 创建PeerConnection PeerConnection* pc = createPeerConnection(); // 设置回调函数 pc->data_received = onReceiveData; // 发送数据 const char* data = "Hello, WebRTC!"; int length = strlen(data); pc->send_data(data, length); // 等待数据接收 webrtc_loop(); // 销毁PeerConnection destroyPeerConnection(pc); // 释放WebRTC资源 webrtc_cleanup(); return 0; } ``` 这段代码使用了WebRTC的C语言库,实现了一个简单的数据传输示例。首先,通过调用`webrtc_init()`函数来初始化WebRTC库。然后,使用`createPeerConnection()`函数创建一个PeerConnection对象,表示与远程端的连接。通过设置回调函数`onReceiveData()`,当接收到数据时会调用该函数。接下来,使用`send_data()`函数发送一段数据。最后,调用`webrtc_loop()`函数开始接收数据,并一直等待,直到关闭程序。在程序结束前,要通过`destroyPeerConnection()`函数销毁PeerConnection对象,最后通过`webrtc_cleanup()`函数释放WebRTC库的资源。 这只是一个简单的示例代码,实际使用WebRTC还需要更多的代码来处理网络连接、音视频编解码等功能。这段代码可以作为一个入门示例,帮助开发者理解WebRTC的基本使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值