niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ |
| 12 | #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 14 | #include <map> |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | 296a0ce | 2018-03-22 15:17:27 -0700 | [diff] [blame] | 16 | #include <string> |
danilchap | b8b6fbb | 2015-12-10 05:05:27 -0800 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 19 | |
Niels Möller | f418bcb | 2018-11-05 13:27:35 +0100 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Danil Chapovalov | d264df5 | 2018-06-14 12:59:38 +0200 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/array_view.h" |
| 23 | #include "api/call/transport.h" |
Per Kjellander | e11b7d2 | 2019-02-21 07:55:59 +0100 | [diff] [blame] | 24 | #include "api/transport/webrtc_key_value_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/rtp_rtcp/include/flexfec_sender.h" |
| 26 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 27 | #include "modules/rtp_rtcp/include/rtp_packet_sender.h" |
Erik Språng | 4580ca2 | 2019-07-04 10:38:43 +0200 | [diff] [blame] | 28 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "modules/rtp_rtcp/source/rtp_packet_history.h" |
| 31 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "rtc_base/constructor_magic.h" |
| 33 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/deprecation.h" |
| 35 | #include "rtc_base/random.h" |
| 36 | #include "rtc_base/rate_statistics.h" |
| 37 | #include "rtc_base/thread_annotations.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | namespace webrtc { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 40 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 41 | class FrameEncryptorInterface; |
michaelt | 4da3044 | 2016-11-17 01:38:43 -0800 | [diff] [blame] | 42 | class OverheadObserver; |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 43 | class RateLimiter; |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 44 | class RtcEventLog; |
| 45 | class RtpPacketToSend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
Erik Språng | 845c6aa | 2019-05-29 13:02:24 +0200 | [diff] [blame] | 47 | class RTPSender { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 48 | public: |
Erik Språng | 67ac9e8 | 2019-10-25 15:24:15 +0200 | [diff] [blame] | 49 | RTPSender(const RtpRtcp::Configuration& config, |
| 50 | RtpPacketHistory* packet_history, |
| 51 | RtpPacketSender* packet_sender); |
Erik Språng | 4580ca2 | 2019-07-04 10:38:43 +0200 | [diff] [blame] | 52 | |
danilchap | 5fb291a | 2016-08-09 07:43:25 -0700 | [diff] [blame] | 53 | ~RTPSender(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 55 | void SetSendingMediaStatus(bool enabled); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 56 | bool SendingMedia() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
danilchap | 71fead2 | 2016-08-18 02:01:49 -0700 | [diff] [blame] | 58 | uint32_t TimestampOffset() const; |
| 59 | void SetTimestampOffset(uint32_t timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 61 | void SetRid(const std::string& rid); |
| 62 | |
Steve Anton | 296a0ce | 2018-03-22 15:17:27 -0700 | [diff] [blame] | 63 | void SetMid(const std::string& mid); |
| 64 | |
danilchap | 5fb291a | 2016-08-09 07:43:25 -0700 | [diff] [blame] | 65 | uint16_t SequenceNumber() const; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 66 | void SetSequenceNumber(uint16_t seq); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
pbos@webrtc.org | 9334ac2 | 2014-11-24 08:25:50 +0000 | [diff] [blame] | 68 | void SetCsrcs(const std::vector<uint32_t>& csrcs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 70 | void SetMaxRtpPacketSize(size_t max_packet_size); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 72 | void SetExtmapAllowMixed(bool extmap_allow_mixed); |
| 73 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 74 | // RTP header extension |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 75 | int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id); |
Sebastian Jansson | f39c815 | 2019-10-14 17:32:21 +0200 | [diff] [blame] | 76 | bool RegisterRtpHeaderExtension(absl::string_view uri, int id); |
stefan | 53b6cc3 | 2017-02-03 08:13:57 -0800 | [diff] [blame] | 77 | bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) const; |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 78 | int32_t DeregisterRtpHeaderExtension(RTPExtensionType type); |
Sebastian Jansson | f39c815 | 2019-10-14 17:32:21 +0200 | [diff] [blame] | 79 | void DeregisterRtpHeaderExtension(absl::string_view uri); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 80 | |
Mirko Bonadei | 999a72a | 2019-07-12 17:33:46 +0000 | [diff] [blame] | 81 | bool SupportsPadding() const; |
| 82 | bool SupportsRtxPayloadPadding() const; |
Erik Språng | 67ac9e8 | 2019-10-25 15:24:15 +0200 | [diff] [blame] | 83 | |
Erik Språng | 67ac9e8 | 2019-10-25 15:24:15 +0200 | [diff] [blame] | 84 | std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 85 | size_t target_size_bytes, |
| 86 | bool media_has_been_sent); |
| 87 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 88 | // NACK. |
Danil Chapovalov | 2800d74 | 2016-08-26 18:48:46 +0200 | [diff] [blame] | 89 | void OnReceivedNack(const std::vector<uint16_t>& nack_sequence_numbers, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 90 | int64_t avg_rtt); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 91 | |
Erik Språng | a12b1d6 | 2018-03-14 12:39:24 +0100 | [diff] [blame] | 92 | int32_t ReSendPacket(uint16_t packet_id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
Steve Anton | 2bac7da | 2019-07-21 15:04:21 -0400 | [diff] [blame] | 94 | // ACK. |
| 95 | void OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number); |
| 96 | void OnReceivedAckOnRtxSsrc(int64_t extended_highest_sequence_number); |
| 97 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 98 | // RTX. |
pbos@webrtc.org | 0b0c241 | 2015-01-13 14:15:15 +0000 | [diff] [blame] | 99 | void SetRtxStatus(int mode); |
| 100 | int RtxStatus() const; |
Erik Språng | c06aef2 | 2019-10-17 13:02:27 +0200 | [diff] [blame] | 101 | absl::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; } |
stefan@webrtc.org | ef92755 | 2014-06-05 08:25:29 +0000 | [diff] [blame] | 102 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 103 | void SetRtxPayloadType(int payload_type, int associated_payload_type); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 104 | |
erikvarga | 2788373 | 2017-05-17 05:08:38 -0700 | [diff] [blame] | 105 | // Size info for header extensions used by FEC packets. |
| 106 | static rtc::ArrayView<const RtpExtensionSize> FecExtensionSizes(); |
| 107 | |
Ilya Nikolaevskiy | 1d037ae | 2018-03-15 15:46:17 +0100 | [diff] [blame] | 108 | // Size info for header extensions used by video packets. |
| 109 | static rtc::ArrayView<const RtpExtensionSize> VideoExtensionSizes(); |
| 110 | |
Danil Chapovalov | 5e57b17 | 2016-09-02 19:15:59 +0200 | [diff] [blame] | 111 | // Create empty packet, fills ssrc, csrcs and reserve place for header |
| 112 | // extensions RtpSender updates before sending. |
| 113 | std::unique_ptr<RtpPacketToSend> AllocatePacket() const; |
| 114 | // Allocate sequence number for provided packet. |
| 115 | // Save packet's fields to generate padding that doesn't break media stream. |
| 116 | // Return false if sending was turned off. |
| 117 | bool AssignSequenceNumber(RtpPacketToSend* packet); |
| 118 | |
erikvarga | 2788373 | 2017-05-17 05:08:38 -0700 | [diff] [blame] | 119 | // Used for padding and FEC packets only. |
danilchap | 5fb291a | 2016-08-09 07:43:25 -0700 | [diff] [blame] | 120 | size_t RtpHeaderLength() const; |
| 121 | uint16_t AllocateSequenceNumber(uint16_t packets_to_send); |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 122 | // Including RTP headers. |
| 123 | size_t MaxRtpPacketSize() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
Erik Språng | 6841d25 | 2019-10-15 14:29:11 +0200 | [diff] [blame] | 125 | uint32_t SSRC() const { return ssrc_; } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
Erik Språng | 6841d25 | 2019-10-15 14:29:11 +0200 | [diff] [blame] | 127 | absl::optional<uint32_t> FlexfecSsrc() const { return flexfec_ssrc_; } |
brandtr | 9dfff29 | 2016-11-14 05:14:50 -0800 | [diff] [blame] | 128 | |
Niels Möller | 6893f3c | 2019-01-31 08:56:26 +0100 | [diff] [blame] | 129 | // Sends packet to |transport_| or to the pacer, depending on configuration. |
Erik Språng | ea55b08 | 2019-10-02 14:57:46 +0200 | [diff] [blame] | 130 | // TODO(bugs.webrtc.org/XXX): Remove in favor of EnqueuePackets(). |
Erik Språng | 70768f4 | 2019-08-27 18:16:26 +0200 | [diff] [blame] | 131 | bool SendToNetwork(std::unique_ptr<RtpPacketToSend> packet); |
| 132 | |
Erik Språng | ea55b08 | 2019-10-02 14:57:46 +0200 | [diff] [blame] | 133 | // Pass a set of packets to RtpPacketSender instance, for paced or immediate |
| 134 | // sending to the network. |
| 135 | void EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets); |
| 136 | |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 137 | void SetRtpState(const RtpState& rtp_state); |
| 138 | RtpState GetRtpState() const; |
| 139 | void SetRtxRtpState(const RtpState& rtp_state); |
| 140 | RtpState GetRtxRtpState() const; |
| 141 | |
sprang | 168794c | 2017-07-06 04:38:06 -0700 | [diff] [blame] | 142 | int64_t LastTimestampTimeMs() const; |
sprang | 168794c | 2017-07-06 04:38:06 -0700 | [diff] [blame] | 143 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 144 | private: |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 145 | std::unique_ptr<RtpPacketToSend> BuildRtxPacket( |
| 146 | const RtpPacketToSend& packet); |
phoglund@webrtc.org | baaf243 | 2012-05-31 10:47:35 +0000 | [diff] [blame] | 147 | |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 148 | bool IsFecPacket(const RtpPacketToSend& packet) const; |
sprang@webrtc.org | ebad765 | 2013-12-05 14:29:02 +0000 | [diff] [blame] | 149 | |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 150 | Clock* const clock_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 151 | Random random_ RTC_GUARDED_BY(send_critsect_); |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 152 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 153 | const bool audio_configured_; |
Niels Möller | 59ab1cf | 2019-02-06 22:48:11 +0100 | [diff] [blame] | 154 | |
Erik Språng | 6841d25 | 2019-10-15 14:29:11 +0200 | [diff] [blame] | 155 | const uint32_t ssrc_; |
| 156 | const absl::optional<uint32_t> rtx_ssrc_; |
Niels Möller | 59ab1cf | 2019-02-06 22:48:11 +0100 | [diff] [blame] | 157 | const absl::optional<uint32_t> flexfec_ssrc_; |
phoglund@webrtc.org | baaf243 | 2012-05-31 10:47:35 +0000 | [diff] [blame] | 158 | |
Erik Språng | 9cdc9cc | 2019-10-28 18:24:32 +0100 | [diff] [blame] | 159 | RtpPacketHistory* const packet_history_; |
| 160 | RtpPacketSender* const paced_sender_; |
Erik Språng | 67ac9e8 | 2019-10-25 15:24:15 +0200 | [diff] [blame] | 161 | |
tommi | ae695e9 | 2016-02-02 08:31:45 -0800 | [diff] [blame] | 162 | rtc::CriticalSection send_critsect_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 164 | bool sending_media_ RTC_GUARDED_BY(send_critsect_); |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 165 | size_t max_packet_size_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
Danil Chapovalov | b3179c7 | 2018-03-22 10:13:07 +0100 | [diff] [blame] | 167 | int8_t last_payload_type_ RTC_GUARDED_BY(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 169 | RtpHeaderExtensionMap rtp_header_extension_map_ |
| 170 | RTC_GUARDED_BY(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 172 | // RTP variables |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 173 | uint32_t timestamp_offset_ RTC_GUARDED_BY(send_critsect_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 174 | bool sequence_number_forced_ RTC_GUARDED_BY(send_critsect_); |
| 175 | uint16_t sequence_number_ RTC_GUARDED_BY(send_critsect_); |
| 176 | uint16_t sequence_number_rtx_ RTC_GUARDED_BY(send_critsect_); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 177 | // RID value to send in the RID or RepairedRID header extension. |
| 178 | std::string rid_ RTC_GUARDED_BY(send_critsect_); |
Steve Anton | 4af9584 | 2018-04-06 11:09:46 -0700 | [diff] [blame] | 179 | // MID value to send in the MID header extension. |
| 180 | std::string mid_ RTC_GUARDED_BY(send_critsect_); |
Steve Anton | 2bac7da | 2019-07-21 15:04:21 -0400 | [diff] [blame] | 181 | // Track if any ACK has been received on the SSRC and RTX SSRC to indicate |
| 182 | // when to stop sending the MID and RID header extensions. |
| 183 | bool ssrc_has_acked_ RTC_GUARDED_BY(send_critsect_); |
| 184 | bool rtx_ssrc_has_acked_ RTC_GUARDED_BY(send_critsect_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 185 | uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(send_critsect_); |
| 186 | int64_t capture_time_ms_ RTC_GUARDED_BY(send_critsect_); |
| 187 | int64_t last_timestamp_time_ms_ RTC_GUARDED_BY(send_critsect_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 188 | bool last_packet_marker_bit_ RTC_GUARDED_BY(send_critsect_); |
| 189 | std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(send_critsect_); |
| 190 | int rtx_ RTC_GUARDED_BY(send_critsect_); |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 191 | // Mapping rtx_payload_type_map_[associated] = rtx. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 192 | std::map<int8_t, int8_t> rtx_payload_type_map_ RTC_GUARDED_BY(send_critsect_); |
Mirko Bonadei | 999a72a | 2019-07-12 17:33:46 +0000 | [diff] [blame] | 193 | bool supports_bwe_extension_ RTC_GUARDED_BY(send_critsect_); |
andresp@webrtc.org | d09d074 | 2014-03-26 14:27:34 +0000 | [diff] [blame] | 194 | |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 195 | RateLimiter* const retransmission_rate_limiter_; |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 196 | |
| 197 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 200 | } // namespace webrtc |
| 201 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 202 | #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ |