blob: cb59bb2e5f5c2190a7b92c6520cef8ad1c869577 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
12#define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013
pwestin@webrtc.org00741872012-01-19 15:56:10 +000014#include <map>
kwiberg84be5112016-04-27 01:19:58 -070015#include <memory>
Steve Anton296a0ce2018-03-22 15:17:27 -070016#include <string>
danilchapb8b6fbb2015-12-10 05:05:27 -080017#include <utility>
18#include <vector>
pwestin@webrtc.org00741872012-01-19 15:56:10 +000019
Niels Möllerf418bcb2018-11-05 13:27:35 +010020#include "absl/strings/string_view.h"
Danil Chapovalovd264df52018-06-14 12:59:38 +020021#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/array_view.h"
23#include "api/call/transport.h"
Per Kjellandere11b7d22019-02-21 07:55:59 +010024#include "api/transport/webrtc_key_value_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/rtp_rtcp/include/flexfec_sender.h"
26#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Erik Språngaa59eca2019-07-24 14:52:55 +020027#include "modules/rtp_rtcp/include/rtp_packet_sender.h"
Erik Språng4580ca22019-07-04 10:38:43 +020028#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/rtp_rtcp/source/rtp_packet_history.h"
31#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/constructor_magic.h"
33#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#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.com470e71d2011-07-07 08:21:25 +000038
niklase@google.com470e71d2011-07-07 08:21:25 +000039namespace webrtc {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000040
Benjamin Wright192eeec2018-10-17 17:27:25 -070041class FrameEncryptorInterface;
michaelt4da30442016-11-17 01:38:43 -080042class OverheadObserver;
sprangcd349d92016-07-13 09:11:28 -070043class RateLimiter;
Danil Chapovalov31e4e802016-08-03 18:27:40 +020044class RtcEventLog;
45class RtpPacketToSend;
niklase@google.com470e71d2011-07-07 08:21:25 +000046
Erik Språng845c6aa2019-05-29 13:02:24 +020047class RTPSender {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000048 public:
Erik Språng67ac9e82019-10-25 15:24:15 +020049 RTPSender(const RtpRtcp::Configuration& config,
50 RtpPacketHistory* packet_history,
51 RtpPacketSender* packet_sender);
Erik Språng4580ca22019-07-04 10:38:43 +020052
danilchap5fb291a2016-08-09 07:43:25 -070053 ~RTPSender();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000055 void SetSendingMediaStatus(bool enabled);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000056 bool SendingMedia() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
danilchap71fead22016-08-18 02:01:49 -070058 uint32_t TimestampOffset() const;
59 void SetTimestampOffset(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
Amit Hilbuch77938e62018-12-21 09:23:38 -080061 void SetRid(const std::string& rid);
62
Steve Anton296a0ce2018-03-22 15:17:27 -070063 void SetMid(const std::string& mid);
64
danilchap5fb291a2016-08-09 07:43:25 -070065 uint16_t SequenceNumber() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 void SetSequenceNumber(uint16_t seq);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
pbos@webrtc.org9334ac22014-11-24 08:25:50 +000068 void SetCsrcs(const std::vector<uint32_t>& csrcs);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
nisse284542b2017-01-10 08:58:32 -080070 void SetMaxRtpPacketSize(size_t max_packet_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000071
Johannes Kron9190b822018-10-29 11:22:05 +010072 void SetExtmapAllowMixed(bool extmap_allow_mixed);
73
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 // RTP header extension
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000075 int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
Sebastian Janssonf39c8152019-10-14 17:32:21 +020076 bool RegisterRtpHeaderExtension(absl::string_view uri, int id);
stefan53b6cc32017-02-03 08:13:57 -080077 bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) const;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000078 int32_t DeregisterRtpHeaderExtension(RTPExtensionType type);
Sebastian Janssonf39c8152019-10-14 17:32:21 +020079 void DeregisterRtpHeaderExtension(absl::string_view uri);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000080
Mirko Bonadei999a72a2019-07-12 17:33:46 +000081 bool SupportsPadding() const;
82 bool SupportsRtxPayloadPadding() const;
Erik Språng67ac9e82019-10-25 15:24:15 +020083
Erik Språng67ac9e82019-10-25 15:24:15 +020084 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
85 size_t target_size_bytes,
86 bool media_has_been_sent);
87
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000088 // NACK.
Danil Chapovalov2800d742016-08-26 18:48:46 +020089 void OnReceivedNack(const std::vector<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000090 int64_t avg_rtt);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000091
Erik Språnga12b1d62018-03-14 12:39:24 +010092 int32_t ReSendPacket(uint16_t packet_id);
niklase@google.com470e71d2011-07-07 08:21:25 +000093
Steve Anton2bac7da2019-07-21 15:04:21 -040094 // ACK.
95 void OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number);
96 void OnReceivedAckOnRtxSsrc(int64_t extended_highest_sequence_number);
97
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 // RTX.
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +000099 void SetRtxStatus(int mode);
100 int RtxStatus() const;
Erik Språngc06aef22019-10-17 13:02:27 +0200101 absl::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; }
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000102
Shao Changbine62202f2015-04-21 20:24:50 +0800103 void SetRtxPayloadType(int payload_type, int associated_payload_type);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000104
erikvarga27883732017-05-17 05:08:38 -0700105 // Size info for header extensions used by FEC packets.
106 static rtc::ArrayView<const RtpExtensionSize> FecExtensionSizes();
107
Ilya Nikolaevskiy1d037ae2018-03-15 15:46:17 +0100108 // Size info for header extensions used by video packets.
109 static rtc::ArrayView<const RtpExtensionSize> VideoExtensionSizes();
110
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200111 // 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
erikvarga27883732017-05-17 05:08:38 -0700119 // Used for padding and FEC packets only.
danilchap5fb291a2016-08-09 07:43:25 -0700120 size_t RtpHeaderLength() const;
121 uint16_t AllocateSequenceNumber(uint16_t packets_to_send);
nisse284542b2017-01-10 08:58:32 -0800122 // Including RTP headers.
123 size_t MaxRtpPacketSize() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
Erik Språng6841d252019-10-15 14:29:11 +0200125 uint32_t SSRC() const { return ssrc_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
Erik Språng6841d252019-10-15 14:29:11 +0200127 absl::optional<uint32_t> FlexfecSsrc() const { return flexfec_ssrc_; }
brandtr9dfff292016-11-14 05:14:50 -0800128
Niels Möller6893f3c2019-01-31 08:56:26 +0100129 // Sends packet to |transport_| or to the pacer, depending on configuration.
Erik Språngea55b082019-10-02 14:57:46 +0200130 // TODO(bugs.webrtc.org/XXX): Remove in favor of EnqueuePackets().
Erik Språng70768f42019-08-27 18:16:26 +0200131 bool SendToNetwork(std::unique_ptr<RtpPacketToSend> packet);
132
Erik Språngea55b082019-10-02 14:57:46 +0200133 // 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.org2bb1bda2014-07-07 13:06:48 +0000137 void SetRtpState(const RtpState& rtp_state);
138 RtpState GetRtpState() const;
139 void SetRtxRtpState(const RtpState& rtp_state);
140 RtpState GetRtxRtpState() const;
141
sprang168794c2017-07-06 04:38:06 -0700142 int64_t LastTimestampTimeMs() const;
sprang168794c2017-07-06 04:38:06 -0700143
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000144 private:
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200145 std::unique_ptr<RtpPacketToSend> BuildRtxPacket(
146 const RtpPacketToSend& packet);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000147
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200148 bool IsFecPacket(const RtpPacketToSend& packet) const;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000149
tommiae695e92016-02-02 08:31:45 -0800150 Clock* const clock_;
danilchap56359be2017-09-07 07:53:45 -0700151 Random random_ RTC_GUARDED_BY(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000152
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 const bool audio_configured_;
Niels Möller59ab1cf2019-02-06 22:48:11 +0100154
Erik Språng6841d252019-10-15 14:29:11 +0200155 const uint32_t ssrc_;
156 const absl::optional<uint32_t> rtx_ssrc_;
Niels Möller59ab1cf2019-02-06 22:48:11 +0100157 const absl::optional<uint32_t> flexfec_ssrc_;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000158
Erik Språng9cdc9cc2019-10-28 18:24:32 +0100159 RtpPacketHistory* const packet_history_;
160 RtpPacketSender* const paced_sender_;
Erik Språng67ac9e82019-10-25 15:24:15 +0200161
tommiae695e92016-02-02 08:31:45 -0800162 rtc::CriticalSection send_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
danilchap56359be2017-09-07 07:53:45 -0700164 bool sending_media_ RTC_GUARDED_BY(send_critsect_);
nisse284542b2017-01-10 08:58:32 -0800165 size_t max_packet_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
Danil Chapovalovb3179c72018-03-22 10:13:07 +0100167 int8_t last_payload_type_ RTC_GUARDED_BY(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
danilchap56359be2017-09-07 07:53:45 -0700169 RtpHeaderExtensionMap rtp_header_extension_map_
170 RTC_GUARDED_BY(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000172 // RTP variables
danilchap56359be2017-09-07 07:53:45 -0700173 uint32_t timestamp_offset_ RTC_GUARDED_BY(send_critsect_);
danilchap56359be2017-09-07 07:53:45 -0700174 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 Hilbuch77938e62018-12-21 09:23:38 -0800177 // RID value to send in the RID or RepairedRID header extension.
178 std::string rid_ RTC_GUARDED_BY(send_critsect_);
Steve Anton4af95842018-04-06 11:09:46 -0700179 // MID value to send in the MID header extension.
180 std::string mid_ RTC_GUARDED_BY(send_critsect_);
Steve Anton2bac7da2019-07-21 15:04:21 -0400181 // 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_);
danilchap56359be2017-09-07 07:53:45 -0700185 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_);
danilchap56359be2017-09-07 07:53:45 -0700188 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 Changbine62202f2015-04-21 20:24:50 +0800191 // Mapping rtx_payload_type_map_[associated] = rtx.
danilchap56359be2017-09-07 07:53:45 -0700192 std::map<int8_t, int8_t> rtx_payload_type_map_ RTC_GUARDED_BY(send_critsect_);
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000193 bool supports_bwe_extension_ RTC_GUARDED_BY(send_critsect_);
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000194
sprangcd349d92016-07-13 09:11:28 -0700195 RateLimiter* const retransmission_rate_limiter_;
terelius429c3452016-01-21 05:42:04 -0800196
197 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198};
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000200} // namespace webrtc
201
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200202#endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_