• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021 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 MODULES_RTP_RTCP_SOURCE_PACKET_SEQUENCER_H_
12 #define MODULES_RTP_RTCP_SOURCE_PACKET_SEQUENCER_H_
13 
14 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
15 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
16 #include "system_wrappers/include/clock.h"
17 
18 namespace webrtc {
19 
20 // Helper class used to assign RTP sequence numbers and populate some fields for
21 // padding packets based on the last sequenced packets.
22 // This class is not thread safe, the caller must provide that.
23 class PacketSequencer {
24  public:
25   // If `require_marker_before_media_padding_` is true, padding packets on the
26   // media ssrc is not allowed unless the last sequenced media packet had the
27   // marker bit set (i.e. don't insert padding packets between the first and
28   // last packets of a video frame).
29   // Packets with unknown SSRCs will be ignored.
30   PacketSequencer(uint32_t media_ssrc,
31                   absl::optional<uint32_t> rtx_ssrc,
32                   bool require_marker_before_media_padding,
33                   Clock* clock);
34 
35   // Assigns sequence number, and in the case of non-RTX padding also timestamps
36   // and payload type.
37   void Sequence(RtpPacketToSend& packet);
38 
set_media_sequence_number(uint16_t sequence_number)39   void set_media_sequence_number(uint16_t sequence_number) {
40     media_sequence_number_ = sequence_number;
41   }
set_rtx_sequence_number(uint16_t sequence_number)42   void set_rtx_sequence_number(uint16_t sequence_number) {
43     rtx_sequence_number_ = sequence_number;
44   }
45 
46   void SetRtpState(const RtpState& state);
47   void PopulateRtpState(RtpState& state) const;
48 
media_sequence_number()49   uint16_t media_sequence_number() const { return media_sequence_number_; }
rtx_sequence_number()50   uint16_t rtx_sequence_number() const { return rtx_sequence_number_; }
51 
52   // Checks whether it is allowed to send padding on the media SSRC at this
53   // time, e.g. that we don't send padding in the middle of a video frame.
54   bool CanSendPaddingOnMediaSsrc() const;
55 
56  private:
57   void UpdateLastPacketState(const RtpPacketToSend& packet);
58   void PopulatePaddingFields(RtpPacketToSend& packet);
59 
60   const uint32_t media_ssrc_;
61   const absl::optional<uint32_t> rtx_ssrc_;
62   const bool require_marker_before_media_padding_;
63   Clock* const clock_;
64 
65   uint16_t media_sequence_number_;
66   uint16_t rtx_sequence_number_;
67 
68   int8_t last_payload_type_;
69   uint32_t last_rtp_timestamp_;
70   int64_t last_capture_time_ms_;
71   int64_t last_timestamp_time_ms_;
72   bool last_packet_marker_bit_;
73 };
74 
75 }  // namespace webrtc
76 
77 #endif  // MODULES_RTP_RTCP_SOURCE_PACKET_SEQUENCER_H_
78