tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

video_receive_stream.h (14099B)


      1 /*
      2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #ifndef CALL_VIDEO_RECEIVE_STREAM_H_
     12 #define CALL_VIDEO_RECEIVE_STREAM_H_
     13 
     14 #include <cstdint>
     15 #include <functional>
     16 #include <limits>
     17 #include <map>
     18 #include <optional>
     19 #include <set>
     20 #include <string>
     21 #include <utility>
     22 #include <vector>
     23 
     24 #include "api/call/transport.h"
     25 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     26 #include "api/crypto/crypto_options.h"
     27 #include "api/crypto/frame_decryptor_interface.h"
     28 #include "api/frame_transformer_interface.h"
     29 #include "api/rtp_headers.h"
     30 #include "api/scoped_refptr.h"
     31 #include "api/units/time_delta.h"
     32 #include "api/units/timestamp.h"
     33 #include "api/video/recordable_encoded_frame.h"
     34 #include "api/video/video_content_type.h"
     35 #include "api/video/video_frame.h"
     36 #include "api/video/video_sink_interface.h"
     37 #include "api/video/video_timing.h"
     38 #include "api/video_codecs/sdp_video_format.h"
     39 #include "call/receive_stream.h"
     40 #include "call/rtp_config.h"
     41 #include "common_video/frame_counts.h"
     42 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
     43 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     44 
     45 namespace webrtc {
     46 
     47 class RtpPacketSinkInterface;
     48 class VideoDecoderFactory;
     49 
     50 class VideoReceiveStreamInterface : public MediaReceiveStreamInterface {
     51 public:
     52  // Class for handling moving in/out recording state.
     53  struct RecordingState {
     54    RecordingState() = default;
     55    explicit RecordingState(
     56        std::function<void(const RecordableEncodedFrame&)> callback)
     57        : callback(std::move(callback)) {}
     58 
     59    // Callback stored from the VideoReceiveStreamInterface. The
     60    // VideoReceiveStreamInterface client should not interpret the attribute.
     61    std::function<void(const RecordableEncodedFrame&)> callback;
     62    // Memento of when a keyframe request was last sent. The
     63    // VideoReceiveStreamInterface client should not interpret the attribute.
     64    std::optional<int64_t> last_keyframe_request_ms;
     65  };
     66 
     67  // TODO(mflodman) Move all these settings to VideoDecoder and move the
     68  // declaration to common_types.h.
     69  struct Decoder {
     70    Decoder(SdpVideoFormat video_format, int payload_type);
     71    Decoder();
     72    Decoder(const Decoder&);
     73    ~Decoder();
     74 
     75    bool operator==(const Decoder& other) const;
     76 
     77    std::string ToString() const;
     78 
     79    SdpVideoFormat video_format;
     80 
     81    // Received RTP packets with this payload type will be sent to this decoder
     82    // instance.
     83    int payload_type = 0;
     84  };
     85 
     86  struct Stats {
     87    Stats();
     88    ~Stats();
     89    std::string ToString(int64_t time_ms) const;
     90 
     91    int network_frame_rate = 0;
     92    int decode_frame_rate = 0;
     93    int render_frame_rate = 0;
     94    uint32_t frames_rendered = 0;
     95 
     96    // Decoder stats.
     97    std::optional<std::string> decoder_implementation_name;
     98    std::optional<bool> power_efficient_decoder;
     99    FrameCounts frame_counts;
    100    int decode_ms = 0;
    101    int max_decode_ms = 0;
    102    int current_delay_ms = 0;
    103    int target_delay_ms = 0;
    104    int jitter_buffer_ms = 0;
    105    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferdelay
    106    TimeDelta jitter_buffer_delay = TimeDelta::Zero();
    107    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbuffertargetdelay
    108    TimeDelta jitter_buffer_target_delay = TimeDelta::Zero();
    109    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferemittedcount
    110    uint64_t jitter_buffer_emitted_count = 0;
    111    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferminimumdelay
    112    TimeDelta jitter_buffer_minimum_delay = TimeDelta::Zero();
    113    int min_playout_delay_ms = 0;
    114    int render_delay_ms = 10;
    115    int64_t interframe_delay_max_ms = -1;
    116    // Frames dropped due to decoding failures or if the system is too slow.
    117    // https://www.w3.org/TR/webrtc-stats/#dom-rtcvideoreceiverstats-framesdropped
    118    uint32_t frames_dropped = 0;
    119    uint32_t frames_decoded = 0;
    120    // https://w3c.github.io/webrtc-stats/#dom-rtcreceivedrtpstreamstats-packetsdiscarded
    121    uint64_t packets_discarded = 0;
    122    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totaldecodetime
    123    TimeDelta total_decode_time = TimeDelta::Zero();
    124    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalprocessingdelay
    125    TimeDelta total_processing_delay = TimeDelta::Zero();
    126 
    127    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalassemblytime
    128    TimeDelta total_assembly_time = TimeDelta::Zero();
    129    uint32_t frames_assembled_from_multiple_packets = 0;
    130 
    131    // Total inter frame delay in seconds.
    132    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalinterframedelay
    133    double total_inter_frame_delay = 0;
    134    // Total squared inter frame delay in seconds^2.
    135    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalsqauredinterframedelay
    136    double total_squared_inter_frame_delay = 0;
    137    int64_t first_frame_received_to_decoded_ms = -1;
    138    std::optional<uint64_t> qp_sum;
    139 
    140    // Corruption score, indicating the probability of corruption. Its value is
    141    // between 0 and 1, where 0 means no corruption and 1 means that the
    142    // compressed frame is corrupted.
    143    // However, note that the corruption score may not accurately reflect
    144    // corruption. E.g. even if the corruption score is 0, the compressed frame
    145    // may still be corrupted and vice versa.
    146    std::optional<double> corruption_score_sum;
    147    std::optional<double> corruption_score_squared_sum;
    148    // Number of frames the `corruption_score` was calculated on. This is
    149    // usually not the same as `frames_decoded`.
    150    uint32_t corruption_score_count = 0;
    151 
    152    int current_payload_type = -1;
    153 
    154    int total_bitrate_bps = 0;
    155 
    156    int width = 0;
    157    int height = 0;
    158 
    159    uint32_t freeze_count = 0;
    160    uint32_t pause_count = 0;
    161    uint32_t total_freezes_duration_ms = 0;
    162    uint32_t total_pauses_duration_ms = 0;
    163 
    164    VideoContentType content_type = VideoContentType::UNSPECIFIED;
    165 
    166    // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp
    167    std::optional<int64_t> estimated_playout_ntp_timestamp_ms;
    168    int sync_offset_ms = std::numeric_limits<int>::max();
    169 
    170    uint32_t ssrc = 0;
    171    std::string c_name;
    172    RtpReceiveStats rtp_stats;
    173    RtcpPacketTypeCounter rtcp_packet_type_counts;
    174    std::optional<RtpReceiveStats> rtx_rtp_stats;
    175 
    176    // Mozilla modification: Init these.
    177    uint32_t rtcp_sender_packets_sent = 0;
    178    uint32_t rtcp_sender_octets_sent = 0;
    179    int64_t rtcp_sender_ntp_timestamp_ms = 0;
    180    int64_t rtcp_sender_remote_ntp_timestamp_ms = 0;
    181 
    182    // Timing frame info: all important timestamps for a full lifetime of a
    183    // single 'timing frame'.
    184    std::optional<webrtc::TimingFrameInfo> timing_frame_info;
    185 
    186    // Remote outbound stats derived by the received RTCP sender reports.
    187    // https://w3c.github.io/webrtc-stats/#remoteoutboundrtpstats-dict*
    188    std::optional<Timestamp> last_sender_report_timestamp;
    189    // TODO: bugs.webrtc.org/370535296 - Remove the utc timestamp when linked
    190    // issue is fixed.
    191    std::optional<Timestamp> last_sender_report_utc_timestamp;
    192    std::optional<Timestamp> last_sender_report_remote_utc_timestamp;
    193    uint32_t sender_reports_packets_sent = 0;
    194    uint64_t sender_reports_bytes_sent = 0;
    195    uint64_t sender_reports_reports_count = 0;
    196  };
    197 
    198  struct Config {
    199   private:
    200    // Access to the copy constructor is private to force use of the Copy()
    201    // method for those exceptional cases where we do use it.
    202    Config(const Config&);
    203 
    204   public:
    205    Config() = delete;
    206    Config(Config&&);
    207    Config(Transport* rtcp_send_transport,
    208           VideoDecoderFactory* decoder_factory = nullptr);
    209    Config& operator=(Config&&);
    210    Config& operator=(const Config&) = delete;
    211    ~Config();
    212 
    213    // Mostly used by tests.  Avoid creating copies if you can.
    214    Config Copy() const { return Config(*this); }
    215 
    216    std::string ToString() const;
    217 
    218    // Decoders for every payload that we can receive.
    219    std::vector<Decoder> decoders;
    220 
    221    // Ownership stays with WebrtcVideoEngine (delegated from PeerConnection).
    222    VideoDecoderFactory* decoder_factory = nullptr;
    223 
    224    // Receive-stream specific RTP settings.
    225    struct Rtp : public ReceiveStreamRtpConfig {
    226      Rtp();
    227      Rtp(const Rtp&);
    228      ~Rtp();
    229      std::string ToString() const;
    230 
    231      // See NackConfig for description.
    232      NackConfig nack;
    233 
    234      // See RtcpMode for description.
    235      RtcpMode rtcp_mode = RtcpMode::kCompound;
    236 
    237      // Extended RTCP settings.
    238      struct RtcpXr {
    239        // True if RTCP Receiver Reference Time Report Block extension
    240        // (RFC 3611) should be enabled.
    241        bool receiver_reference_time_report = false;
    242      } rtcp_xr;
    243 
    244      // How to request keyframes from a remote sender. Applies only if lntf is
    245      // disabled.
    246      KeyFrameReqMethod keyframe_method = KeyFrameReqMethod::kPliRtcp;
    247 
    248      // See draft-alvestrand-rmcat-remb for information.
    249      bool remb = false;
    250 
    251      bool tmmbr = false;
    252 
    253      // See LntfConfig for description.
    254      LntfConfig lntf;
    255 
    256      // Payload types for ULPFEC and RED, respectively.
    257      int ulpfec_payload_type = -1;
    258      int red_payload_type = -1;
    259 
    260      // SSRC for retransmissions.
    261      uint32_t rtx_ssrc = 0;
    262 
    263      // Set if the stream is protected using FlexFEC.
    264      bool protected_by_flexfec = false;
    265 
    266      // Optional callback sink to support additional packet handlers such as
    267      // FlexFec.
    268      RtpPacketSinkInterface* packet_sink_ = nullptr;
    269 
    270      // Map from rtx payload type -> media payload type.
    271      // For RTX to be enabled, both an SSRC and this mapping are needed.
    272      std::map<int, int> rtx_associated_payload_types;
    273 
    274      // Payload types that should be depacketized using raw depacketizer
    275      // (payload header will not be parsed and must not be present, additional
    276      // meta data is expected to be present in generic frame descriptor
    277      // RTP header extension).
    278      std::set<int> raw_payload_types;
    279 
    280      RtcpEventObserver* rtcp_event_observer = nullptr;
    281    } rtp;
    282 
    283    // Transport for outgoing packets (RTCP).
    284    Transport* rtcp_send_transport = nullptr;
    285 
    286    // Must always be set.
    287    VideoSinkInterface<VideoFrame>* renderer = nullptr;
    288 
    289    // Expected delay needed by the renderer, i.e. the frame will be delivered
    290    // this many milliseconds, if possible, earlier than the ideal render time.
    291    int render_delay_ms = 10;
    292 
    293    // If false, pass frames on to the renderer as soon as they are
    294    // available.
    295    bool enable_prerenderer_smoothing = true;
    296 
    297    // Identifier for an A/V synchronization group. Empty string to disable.
    298    // TODO(pbos): Synchronize streams in a sync group, not just video streams
    299    // to one of the audio streams.
    300    std::string sync_group;
    301 
    302    // An optional custom frame decryptor that allows the entire frame to be
    303    // decrypted in whatever way the caller choses. This is not required by
    304    // default.
    305    scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor;
    306 
    307    // Per PeerConnection cryptography options.
    308    CryptoOptions crypto_options;
    309 
    310    scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
    311  };
    312 
    313  // TODO(pbos): Add info on currently-received codec to Stats.
    314  virtual Stats GetStats() const = 0;
    315 
    316  // Sets a base minimum for the playout delay. Base minimum delay sets lower
    317  // bound on minimum delay value determining lower bound on playout delay.
    318  //
    319  // Returns true if value was successfully set, false overwise.
    320  virtual bool SetBaseMinimumPlayoutDelayMs(int delay_ms) = 0;
    321 
    322  // Returns current value of base minimum delay in milliseconds.
    323  virtual int GetBaseMinimumPlayoutDelayMs() const = 0;
    324 
    325  // Sets and returns recording state. The old state is moved out
    326  // of the video receive stream and returned to the caller, and `state`
    327  // is moved in. If the state's callback is set, it will be called with
    328  // recordable encoded frames as they arrive.
    329  // If `generate_key_frame` is true, the method will generate a key frame.
    330  // When the function returns, it's guaranteed that all old callouts
    331  // to the returned callback has ceased.
    332  // Note: the client should not interpret the returned state's attributes, but
    333  // instead treat it as opaque data.
    334  virtual RecordingState SetAndGetRecordingState(RecordingState state,
    335                                                 bool generate_key_frame) = 0;
    336 
    337  // Cause eventual generation of a key frame from the sender.
    338  virtual void GenerateKeyFrame() = 0;
    339 
    340  // Sets or clears a flexfec RTP sink. This affects `rtp.packet_sink_` and
    341  // `rtp.protected_by_flexfec` parts of the configuration. Must be called on
    342  // the packet delivery thread.
    343  // TODO(bugs.webrtc.org/11993): Packet delivery thread today means `worker
    344  // thread` but will be `network thread`.
    345  virtual void SetFlexFecProtection(RtpPacketSinkInterface* flexfec_sink) = 0;
    346 
    347  // Turns on/off loss notifications. Must be called on the packet delivery
    348  // thread.
    349  virtual void SetLossNotificationEnabled(bool enabled) = 0;
    350 
    351  // Modify `rtp.nack.rtp_history_ms` post construction. Setting this value
    352  // to 0 disables nack.
    353  // Must be called on the packet delivery thread.
    354  virtual void SetNackHistory(TimeDelta history) = 0;
    355 
    356  virtual void SetProtectionPayloadTypes(int red_payload_type,
    357                                         int ulpfec_payload_type) = 0;
    358 
    359  virtual void SetRtcpXr(Config::Rtp::RtcpXr rtcp_xr) = 0;
    360 
    361  virtual void SetAssociatedPayloadTypes(
    362      std::map<int, int> associated_payload_types) = 0;
    363 
    364  virtual void UpdateRtxSsrc(uint32_t ssrc) = 0;
    365 
    366 protected:
    367  virtual ~VideoReceiveStreamInterface() {}
    368 };
    369 
    370 }  // namespace webrtc
    371 
    372 #endif  // CALL_VIDEO_RECEIVE_STREAM_H_