blob: 67255164f3ab7cc1076d41d91aeb52067311ac61 [file] [log] [blame]
Erik Språng77ee8542021-02-22 13:34:31 +01001/*
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
18namespace 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.
23class 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 PacketSequencer(uint32_t media_ssrc,
30 uint32_t rtx_ssrc,
31 bool require_marker_before_media_padding,
32 Clock* clock);
33
34 // Assigns sequence number, and in the case of non-RTX padding also timestamps
35 // and payload type.
36 // Returns false if sequencing failed, which it can do for instance if the
37 // packet to squence is padding on the media ssrc, but the media is mid frame
38 // (the last marker bit is false).
39 bool Sequence(RtpPacketToSend& packet);
40
41 void set_media_sequence_number(uint16_t sequence_number) {
42 media_sequence_number_ = sequence_number;
43 }
44 void set_rtx_sequence_number(uint16_t sequence_number) {
45 rtx_sequence_number_ = sequence_number;
46 }
47
48 void SetRtpState(const RtpState& state);
49 void PupulateRtpState(RtpState& state) const;
50
51 uint16_t media_sequence_number() const { return media_sequence_number_; }
52 uint16_t rtx_sequence_number() const { return rtx_sequence_number_; }
53
54 private:
55 void UpdateLastPacketState(const RtpPacketToSend& packet);
56 bool PopulatePaddingFields(RtpPacketToSend& packet);
57
58 const uint32_t media_ssrc_;
59 const uint32_t rtx_ssrc_;
60 const bool require_marker_before_media_padding_;
61 Clock* const clock_;
62
63 uint16_t media_sequence_number_;
64 uint16_t rtx_sequence_number_;
65
66 int8_t last_payload_type_;
67 uint32_t last_rtp_timestamp_;
68 int64_t last_capture_time_ms_;
69 int64_t last_timestamp_time_ms_;
70 bool last_packet_marker_bit_;
71};
72
73} // namespace webrtc
74
75#endif // MODULES_RTP_RTCP_SOURCE_PACKET_SEQUENCER_H_