美文网首页
WebRTC RTP/RTCP 源码分析(二):RTP 的接收

WebRTC RTP/RTCP 源码分析(二):RTP 的接收

作者: 酸辣粉多加辣椒 | 来源:发表于2019-04-08 13:26 被阅读0次

基于 Chromium M69版本

当 RTP 包完成拆分后,BaseChannel 把包到达时间抓换成微秒,然后通知 MediaChannel 处理收到的包。

// src/pc/channel.cc
void BaseChannel::OnRtpPacket(const webrtc::RtpPacketReceived& parsed_packet) {
  // 重构了 parsed_packet 里的 PacketTime
  OnPacketReceived(/*rtcp=*/false, parsed_packet.Buffer(), packet_time);
}
void BaseChannel::OnPacketReceived(bool rtcp,
                                   const rtc::CopyOnWriteBuffer& packet,
                                   const rtc::PacketTime& packet_time) {
  // 如果是第一个 RTP 包则进行额外处理
  invoker_.AsyncInvoke<void>(
      RTC_FROM_HERE, worker_thread_,
      Bind(&BaseChannel::ProcessPacket, this, rtcp, packet, packet_time));
}
void BaseChannel::ProcessPacket(bool rtcp,
                                const rtc::CopyOnWriteBuffer& packet,
                                const rtc::PacketTime& packet_time) {
  media_channel_->OnPacketReceived(&data, packet_time);
}

MediaChannel 是个父类,由 VoiceMediaChannel,VideoMediaChannel和DataMediaChannel实现,一下主要讲视频方面。WebRtcVideoChannel 继承了 VideoMediaChannel,实现了 OnPacketReceived,响应 BaseChannel 的调用。

WebRtcVideoChannel 通知 Call 递交 RTP 包。如果递交结果为DELIVERY_OK则说明包被正常递交,DELIVERY_PACKET_ERROR说明包无法正常解析,DELIVERY_UNKNOWN_SSRC说明没找到该包的 ssrc,最后返回。

// src/media/engine/webrtcvideoengine.cc
void WebRtcVideoChannel::OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
                                          const rtc::PacketTime& packet_time) {
  const webrtc::PacketReceiver::DeliveryStatus delivery_result =
      call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, *packet,
                                       webrtc_packet_time);
}

Call 判断包是否能正常解析,是否能找到对应 ssrc,计算包到达时间,计算方式为 (timestamp_us + 500) / 1000,调用 RemoteEstimatorProxy 记录每个包序列号对应的到达时间用于 send-side congestion control,然后把视频包传递给 RtpStreamReceiverController 进一步处理。之后统计每秒收到的数据总大小和每秒收到的视频数据总大小。

// src/call/call.cc
PacketReceiver::DeliveryStatus Call::DeliverPacket(
    MediaType media_type,
    rtc::CopyOnWriteBuffer packet,
    const PacketTime& packet_time) {
  // 如果是 RTCP 包则调用 DeliverRtcp 处理
  return DeliverRtp(media_type, std::move(packet), packet_time);
}
PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type,
                                                rtc::CopyOnWriteBuffer packet,
                                                const PacketTime& packet_time) {

  NotifyBweOfReceivedPacket(parsed_packet, media_type);
  video_receiver_controller_.OnRtpPacket(parsed_packet);
}

RtpStreamReceiverController 没有其他操作直接传给 RtpDemuxer 处理包。

// src/call/rtp_stream_receiver_controller.cc
bool RtpStreamReceiverController::OnRtpPacket(const RtpPacketReceived& packet) {
  rtc::CritScope cs(&lock_);
  return demuxer_.OnRtpPacket(packet);
}

RtpDemuxer 根据 MID、RSID 或者 payload 类型获取对应的 RtpPacketSinkInterface。
视频包由 RtpVideoStreamReceiver 继续处理,其余还有 RtxReceiveStream、FlexfecReceiveStream、ChannelProxy。

// src/call/rtp_demuxer.cc
bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) {
  RtpPacketSinkInterface* sink = ResolveSink(packet);
  if (sink != nullptr) {
    sink->OnRtpPacket(packet);
    return true;
  }
  return false;
}

RtpVideoStreamReceiver 负责处理常规 RTP 包和由 FlexFEC 恢复的包。首先获取包的 Header 信息,然后通知 RtpReceiver 正式接收 RTP 包,记录非重传包的接收数据。最后调用二级sink继续处理。

// src/video/rtp_video_stream_receiver.cc
void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
  packet.GetHeader(&header);
  ReceivePacket(packet.data(), packet.size(), header);
  rtp_receive_statistics_->IncomingPacket(header, packet.size(),
                                          IsPacketRetransmitted(header));
  secondary_sink->OnRtpPacket(packet);
}
void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet,
                                           size_t packet_length,
                                           const RTPHeader& header) {
  if (header.payloadType == config_.rtp.red_payload_type) {
    ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
    return;
  }
  const uint8_t* payload = packet + header.headerLength;
  assert(packet_length >= header.headerLength);
  size_t payload_length = packet_length - header.headerLength;
  const auto pl =
      rtp_payload_registry_.PayloadTypeToPayload(header.payloadType);
  if (pl) {
    rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
                                     pl->typeSpecific);
  }
}

RtpReceiver 类由 RtpReceiverImpl 进行实现,响应上面对 IncomingRtpPacket 的调用。RtpReceiver 负责处理 audio_level 信息,通知 RTPReceiverStrategy 解析 RTP 包,并区别迟到和重传的包。

bool RtpReceiverImpl::IncomingRtpPacket(const RTPHeader& rtp_header,
                                        const uint8_t* payload,
                                        size_t payload_length,
                                        PayloadUnion payload_specific) {
  int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
      &webrtc_rtp_header, payload_specific, payload, payload_length,
      clock_->TimeInMilliseconds());
}

RTPReceiverStrategy 类由 RTPReceiverVideo 和 RTPReceiverAudio 进行实现,视频 RTP 包由 RTPReceiverVideo 负责具体解析 rtp_header 和 rtp_payload。

int32_t RTPReceiverVideo::ParseRtpPacket(WebRtcRTPHeader* rtp_header,
                                         const PayloadUnion& specific_payload,
                                         const uint8_t* payload,
                                         size_t payload_length,
                                         int64_t timestamp_ms) {}

相关文章

网友评论

      本文标题:WebRTC RTP/RTCP 源码分析(二):RTP 的接收

      本文链接:https://www.haomeiwen.com/subject/ykpuiqtx.html