【WebRTC】视频发送链路中类的简单分析(上)

[复制链接]
发表于 2025-11-23 23:07:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次


      
  • 1.视频流发送(VideoSendStream)  

    •    
    • 1.1 质量限定(QualityLimitationReason)   
    • 1.2 RTP设置项(RtpConfig)   
    • 1.3 视频流编码器设置项(VideoStreamEncoderSettings)   
    • 1.4 传输(Transport)   
       
  • 2.视频流发送实现(VideoSendStreamImpl)  

    •    
    • 2.1 码率分配监测器(BitrateAllocatorObserver)   
    • 2.2 编码罗致器(VideoStreamEncoderInterface::EncoderSink)   
       
  • 3.RTP视频发送器(RtpVideoSender)  
  • 4.RTP发送视频(RTPSenderVideo)  

    •    
    • 4.1 RTPSenderVideo::SendEncodedImage()   
    • 4.2 RTPSenderVideo::SendVideo   
    • 4.3 RTPSenderVideo:ogAndSendToNetwork   
       
  • 5.RTP发送器(RTPSender)
视频流在收罗编码之后,必要通过RTP协议举行发送,下面纪录与视频流发送干系的一些类的声明,梳理视频流发送过程中,差异类的继续关系
1.视频流发送(VideoSendStream)

WebRTC中将已编码视频帧传送出去的类为VideoSendStream,其声明位于call/video_send_stream.h中,这个类声明的初志为一个基类,此中声明白一系列视频流发送的纯虚函数,以及视频流发送状态和设置干系的变量。大抵的内容包罗:
(1)视频流的状态(范例,长宽,延时,帧率,帧数,码率等)
(2)发送器状态(编码实例名称,输入帧率,编码帧率,帧数,丢帧数,质量限定等)
(3)发送器设置项(RTP设置项,数据包传输,带宽测试等)
(4)发送器函数(发送,克制,添加资源,天生关键帧)
视频流的范例分为3类,kMedia是利用RTP协议传输的正常流,kRtx(Retransmission)是重传流,kFlexfec专门用于Flexfec。视频流状态当中的帧干系信息,与发送器状态当中的帧干系不完全划一,由于发送器还要思量带宽,拥塞控制等标题,大概会举行丢包或质量限定,二者应该是在动态厘革中形成划一。
视频发送的流程大抵为:设置发送器,获取已编码帧信息,开启发送器,发送帧,关闭发送器
  1. class VideoSendStream {
  2. public:
  3.   // Multiple StreamStats objects are present if simulcast is used (multiple
  4.   // kMedia streams) or if RTX or FlexFEC is negotiated. Multiple SVC layers, on
  5.   // the other hand, does not cause additional StreamStats.
  6.   /*
  7.         如果使用了Simulcast(多个kMedia流),或者协商了RTX或FlexFEC,
  8.         就会出现多个StreamStats对象。另一方面,多个SVC层并不会导致额外的StreamStats
  9.         PS:
  10.         Simulcast表示可以对外传输多个视频流,每个视频流的分辨率和码率不同,以适应不同的用户群体或网络条件
  11.         其实现方式为,使用多个编码实例对同一个视频源,按照不同编码策略进行编码,每条流都有独特的SSRC
  12.   */
  13.   struct StreamStats {
  14.     enum class StreamType {
  15.       // A media stream is an RTP stream for audio or video. Retransmissions and
  16.       // FEC is either sent over the same SSRC or negotiated to be sent over
  17.       // separate SSRCs, in which case separate StreamStats objects exist with
  18.       // references to this media stream's SSRC.
  19.       /*
  20.                 一个媒体流是用于音频或视频的RTP流。重传和FEC(前向纠错)可以通过相同的SSRC发送,
  21.                 或者协商后通过不同的SSRC发送,在后一种情况下,会存在引用这个媒体流SSRC的独立StreamStats对象
  22.           */
  23.       kMedia,
  24.       // RTX streams are streams dedicated to retransmissions. They have a
  25.       // dependency on a single kMedia stream: `referenced_media_ssrc`.
  26.       /*
  27.                 RTX流是专门用于重传的流。它们依赖于单个kMedia流:`referenced_media_ssrc`
  28.           */
  29.       kRtx,
  30.       // FlexFEC streams are streams dedicated to FlexFEC. They have a
  31.       // dependency on a single kMedia stream: `referenced_media_ssrc`.
  32.       /*
  33.                 FlexFEC流是专门用于FlexFEC的流。它们依赖于单个kMedia流:`referenced_media_ssrc`
  34.           */
  35.       kFlexfec,
  36.     };
  37.     StreamStats();
  38.     ~StreamStats();
  39.     std::string ToString() const;
  40.     StreamType type = StreamType::kMedia;
  41.     // If `type` is kRtx or kFlexfec this value is present. The referenced SSRC
  42.     // is the kMedia stream that this stream is performing retransmissions or
  43.     // FEC for. If `type` is kMedia, this value is null.
  44.     /*
  45.                 如果`type`是kRtx或kFlexfec,这个值是存在的。被引用的SSRC是这个流正在执行重传
  46.                 或FEC的kMedia流。如果`type`是kMedia,这个值是空的
  47.         */
  48.     std::optional<uint32_t> referenced_media_ssrc;
  49.     // 帧数量
  50.     FrameCounts frame_counts;
  51.     // 宽度
  52.     int width = 0;
  53.     // 高度
  54.     int height = 0;
  55.     // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
  56.     // 整体码率
  57.     int total_bitrate_bps = 0;
  58.     // 重传比特率
  59.     int retransmit_bitrate_bps = 0;
  60.     // `avg_delay_ms` and `max_delay_ms` are only used in tests. Consider
  61.     // deleting.
  62.     // 平均延时
  63.     int avg_delay_ms = 0;
  64.     // 最大延时
  65.     int max_delay_ms = 0;
  66.     // 用于RTP数据统计
  67.     StreamDataCounters rtp_stats;
  68.     // 用于RTCP包类型的数据统计
  69.     RtcpPacketTypeCounter rtcp_packet_type_counts;
  70.     // A snapshot of the most recent Report Block with additional data of
  71.     // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
  72.     // 一个包含对统计数据感兴趣的额外信息的最新报告块的快照。用于实现`RTCRemoteInboundRtpStreamStats`
  73.     std::optional<ReportBlockData> report_block_data;
  74.     // 编码帧率
  75.     double encode_frame_rate = 0.0;
  76.     // 已编码帧
  77.     int frames_encoded = 0;
  78.     std::optional<uint64_t> qp_sum;
  79.     // 整体编码耗时,以ms为单位
  80.     uint64_t total_encode_time_ms = 0;
  81.     // 总体已编码比特目标
  82.     uint64_t total_encoded_bytes_target = 0;
  83.     uint32_t huge_frames_sent = 0;
  84.     std::optional<ScalabilityMode> scalability_mode;
  85.   };
  86.   // 下面是一些发送视频流的状态信息
  87.   struct Stats {
  88.     Stats();
  89.     ~Stats();
  90.     std::string ToString(int64_t time_ms) const;
  91.     // 编码实例名称
  92.     std::optional<std::string> encoder_implementation_name;
  93.     // 输入帧率
  94.     double input_frame_rate = 0;
  95.     // 编码帧率
  96.     int encode_frame_rate = 0;
  97.     // 平均编码耗时,以ms为单位
  98.     int avg_encode_time_ms = 0;
  99.     // 编码器使用率百分比
  100.     int encode_usage_percent = 0;
  101.     // 已编码帧数量
  102.     uint32_t frames_encoded = 0;
  103.     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
  104.     // 总体编码耗时,以ms为单位
  105.     uint64_t total_encode_time_ms = 0;
  106.     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
  107.     // 编码器目标总比特数
  108.     uint64_t total_encoded_bytes_target = 0;
  109.     // 帧数
  110.     uint32_t frames = 0;
  111.     // 由于采集问题而丢帧的数量
  112.     uint32_t frames_dropped_by_capturer = 0;
  113.     // 由于时间戳问题而丢帧的数量
  114.     uint32_t frames_dropped_by_bad_timestamp = 0;
  115.     // 由于编码队列太长而丢帧的数量
  116.     uint32_t frames_dropped_by_encoder_queue = 0;
  117.     // 由于码率限制而丢帧的数量
  118.     uint32_t frames_dropped_by_rate_limiter = 0;
  119.     // 由于拥塞窗口而丢帧的数量
  120.     uint32_t frames_dropped_by_congestion_window = 0;
  121.     // 由于编码器问题而丢帧的数量
  122.     uint32_t frames_dropped_by_encoder = 0;
  123.     // Bitrate the encoder is currently configured to use due to bandwidth
  124.     // limitations.
  125.     // 由于带宽限制,编码器当前配置使用的比特率
  126.     int target_media_bitrate_bps = 0;
  127.     // Bitrate the encoder is actually producing.
  128.     // 由编码器实际产生的码率
  129.     int media_bitrate_bps = 0;
  130.     // 流是否暂停
  131.     bool suspended = false;
  132.     // 是否由于bandwidth而降低分辨率
  133.     bool bw_limited_resolution = false;
  134.     // 是否由于CPU而降低分辨率
  135.     bool cpu_limited_resolution = false;
  136.     // 是否由于bandwidth而降低帧率
  137.     bool bw_limited_framerate = false;
  138.     // 是否由于CPU而减低帧率
  139.     bool cpu_limited_framerate = false;
  140.     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
  141.     // 质量限制原因
  142.     QualityLimitationReason quality_limitation_reason =
  143.         QualityLimitationReason::kNone;
  144.     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
  145.     // 记录由于不同原因导致质量下降的持续时间
  146.     std::map<QualityLimitationReason, int64_t> quality_limitation_durations_ms;
  147.     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges
  148.     // 记录由于质量限制而导致分辨率变化的次数
  149.     uint32_t quality_limitation_resolution_changes = 0;
  150.     // Total number of times resolution as been requested to be changed due to
  151.     // CPU/quality adaptation.
  152.     // 由于CPU/质量适应,请求改变分辨率的总次数
  153.     int number_of_cpu_adapt_changes = 0;
  154.     int number_of_quality_adapt_changes = 0;
  155.     // 是否进入了低分辨率状态
  156.     bool has_entered_low_resolution = false;
  157.     // 子流
  158.     std::map<uint32_t, StreamStats> substreams;
  159.     // 内容的类型
  160.     webrtc::VideoContentType content_type =
  161.         webrtc::VideoContentType::UNSPECIFIED;
  162.     // 发送出去的视频帧总数
  163.     uint32_t frames_sent = 0;
  164.     // 发送出去的“巨大”视频帧的数量
  165.     uint32_t huge_frames_sent = 0;
  166.     // 是否处于节能模式
  167.     std::optional<bool> power_efficient_encoder;
  168.   };
  169.   // 下面是一些配置
  170.   struct Config {
  171.    public:
  172.     Config() = delete;
  173.     Config(Config&&);
  174.     explicit Config(Transport* send_transport);
  175.     Config& operator=(Config&&);
  176.     Config& operator=(const Config&) = delete;
  177.     ~Config();
  178.     // Mostly used by tests.  Avoid creating copies if you can.
  179.     Config Copy() const { return Config(*this); }
  180.     std::string ToString() const;
  181.     // RTP配置
  182.     RtpConfig rtp;
  183.     // 编码器设置
  184.     VideoStreamEncoderSettings encoder_settings;
  185.     // Time interval between RTCP report for video
  186.     // 视频RTCP报告的时间间隔
  187.     int rtcp_report_interval_ms = 1000;
  188.     // Transport for outgoing packets.
  189.     // 出站数据包的传输
  190.     Transport* send_transport = nullptr;
  191.     // Expected delay needed by the renderer, i.e. the frame will be delivered
  192.     // this many milliseconds, if possible, earlier than expected render time.
  193.     // Only valid if `local_renderer` is set.
  194.     /*
  195.                 渲染器所需的预期延迟,即如果可能的话,帧将比预期渲染时间提前这么多毫秒交付。
  196.                 只有当设置了`local_renderer`时,此值才有效。
  197.         */
  198.     int render_delay_ms = 0;
  199.     // Target delay in milliseconds. A positive value indicates this stream is
  200.     // used for streaming instead of a real-time call.
  201.     // 目标延迟时间,以毫秒为单位。正值表示此流用于流媒体传输,而不是实时通话
  202.     int target_delay_ms = 0;
  203.     // True if the stream should be suspended when the available bitrate fall
  204.     // below the minimum configured bitrate. If this variable is false, the
  205.     // stream may send at a rate higher than the estimated available bitrate.
  206.     // 如果可用比特率下降到最低配置的比特率以下时应该暂停流,则为真
  207.     // 如果这个变量为假,流可能会以高于估计的可用比特率的速度发送
  208.     bool suspend_below_min_bitrate = false;
  209.     // Enables periodic bandwidth probing in application-limited region.
  210.     // 启用在应用受限区域的周期性带宽探测
  211.     bool periodic_alr_bandwidth_probing = false;
  212.     // An optional custom frame encryptor that allows the entire frame to be
  213.     // encrypted in whatever way the caller chooses. This is not required by
  214.     // default.
  215.     // 一个可选的自定义帧加密器,允许整个帧按照调用者选择的任何方式进行加密。这不是默认必需的
  216.     rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor;
  217.     // An optional encoder selector provided by the user.
  218.     // Overrides VideoEncoderFactory::GetEncoderSelector().
  219.     // Owned by RtpSenderBase.
  220.     /*
  221.                 用户提供的一个可选编码器选择器。
  222.                 覆盖了 VideoEncoderFactory::GetEncoderSelector()。
  223.                 由 RtpSenderBase 拥有。
  224.         */
  225.     VideoEncoderFactory::EncoderSelectorInterface* encoder_selector = nullptr;
  226.     // Per PeerConnection cryptography options.
  227.     // 每个PeerConnection的加密选项
  228.     CryptoOptions crypto_options;
  229.         // 帧转换器
  230.     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
  231.    private:
  232.     // Access to the copy constructor is private to force use of the Copy()
  233.     // method for those exceptional cases where we do use it.
  234.     Config(const Config&);
  235.   };
  236.   // Starts stream activity.
  237.   // When a stream is active, it can receive, process and deliver packets.
  238.   // 开启一个流,此时流可以进行接收,处理和传输数据包
  239.   virtual void Start() = 0;
  240.   // Stops stream activity.
  241.   // When a stream is stopped, it can't receive, process or deliver packets.
  242.   // 停止一个流,此时流不能接收,处理和传输数据包
  243.   virtual void Stop() = 0;
  244.   // Accessor for determining if the stream is active. This is an inexpensive
  245.   // call that must be made on the same thread as `Start()` and `Stop()` methods
  246.   // are called on and will return `true` iff activity has been started
  247.   // via `Start()`.
  248.   // 用于确定流是否处于活动状态的访问器。这是一个低成本的调用,必须在与`Start()`和
  249.   // `Stop()`方法调用相同的线程上进行,并且仅当通过`Start()`启动了活动时才会返回`true`
  250.   virtual bool started() = 0;
  251.   // If the resource is overusing, the VideoSendStream will try to reduce
  252.   // resolution or frame rate until no resource is overusing.
  253.   // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
  254.   // is moved to Call this method could be deleted altogether in favor of
  255.   // Call-level APIs only.
  256.   // 如果资源使用过量,VideoSendStream将尝试降低分辨率或帧率,直到没有资源使用过量
  257.   virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
  258.   virtual std::vector<rtc::scoped_refptr<Resource>>
  259.   GetAdaptationResources() = 0;
  260.   virtual void SetSource(
  261.       rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
  262.       const DegradationPreference& degradation_preference) = 0;
  263.   // Set which streams to send. Must have at least as many SSRCs as configured
  264.   // in the config. Encoder settings are passed on to the encoder instance along
  265.   // with the VideoStream settings.
  266.   // 设置要发送的流。必须至少有与配置中配置的一样多的SSRCs。编码器设置会与VideoStream设置一起传递给编码器实例
  267.   virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
  268.   virtual void ReconfigureVideoEncoder(VideoEncoderConfig config,
  269.                     
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表