blob: ffbcb817e709cb569da1ec09c0e5d43adfd14735 [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
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013
danilchapb8b6fbb2015-12-10 05:05:27 -080014#include <list>
pwestin@webrtc.org00741872012-01-19 15:56:10 +000015#include <map>
kwiberg84be5112016-04-27 01:19:58 -070016#include <memory>
danilchapb8b6fbb2015-12-10 05:05:27 -080017#include <utility>
18#include <vector>
pwestin@webrtc.org00741872012-01-19 15:56:10 +000019
kwiberg4485ffb2016-04-26 08:14:39 -070020#include "webrtc/base/constructormagic.h"
tommiae695e92016-02-02 08:31:45 -080021#include "webrtc/base/criticalsection.h"
danilchap47a740b2015-12-15 00:30:07 -080022#include "webrtc/base/random.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000023#include "webrtc/base/thread_annotations.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000024#include "webrtc/common_types.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
aluebsa49f1102016-07-08 11:01:59 -070026#include "webrtc/modules/rtp_rtcp/source/bitrate.h"
isheriff6b4b5f32016-06-08 00:24:21 -070027#include "webrtc/modules/rtp_rtcp/source/playout_delay_oracle.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000028#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000029#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000030#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
mflodmanfcf54bd2015-04-14 21:28:08 +020031#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000032#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
pbos2d566682015-09-28 09:59:31 -070033#include "webrtc/transport.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000034
niklase@google.com470e71d2011-07-07 08:21:25 +000035namespace webrtc {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000036
niklase@google.com470e71d2011-07-07 08:21:25 +000037class RTPSenderAudio;
38class RTPSenderVideo;
terelius429c3452016-01-21 05:42:04 -080039class RtcEventLog;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000041class RTPSenderInterface {
42 public:
43 RTPSenderInterface() {}
44 virtual ~RTPSenderInterface() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000045
pbos@webrtc.org2f446732013-04-08 11:08:41 +000046 virtual uint32_t SSRC() const = 0;
47 virtual uint32_t Timestamp() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000049 virtual int32_t BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000050 int8_t payload_type,
51 bool marker_bit,
52 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000053 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000054 bool timestamp_provided = true,
55 bool inc_sequence_number = true) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000056
isheriff6b4b5f32016-06-08 00:24:21 -070057 // This returns the expected header length taking into consideration
58 // the optional RTP header extensions that may not be currently active.
59 virtual size_t RtpHeaderLength() const = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +020060 // Returns the next sequence number to use for a packet and allocates
61 // 'packets_to_send' number of sequence numbers. It's important all allocated
62 // sequence numbers are used in sequence to avoid perceived packet loss.
63 virtual uint16_t AllocateSequenceNumber(uint16_t packets_to_send) = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000064 virtual uint16_t SequenceNumber() const = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000065 virtual size_t MaxPayloadLength() const = 0;
66 virtual size_t MaxDataPayloadLength() const = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000067 virtual uint16_t ActualSendBitrateKbit() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
sprangebbf8a82015-09-21 15:11:14 -070069 virtual int32_t SendToNetwork(uint8_t* data_buffer,
70 size_t payload_length,
71 size_t rtp_header_length,
72 int64_t capture_time_ms,
73 StorageType storage,
74 RtpPacketSender::Priority priority) = 0;
guoweis@webrtc.org45362892015-03-04 22:55:15 +000075
76 virtual bool UpdateVideoRotation(uint8_t* rtp_packet,
77 size_t rtp_packet_length,
78 const RTPHeader& rtp_header,
79 VideoRotation rotation) const = 0;
80 virtual bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) = 0;
isheriff6b4b5f32016-06-08 00:24:21 -070081 virtual bool ActivateCVORtpHeaderExtension() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000082};
83
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000084class RTPSender : public RTPSenderInterface {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000085 public:
Peter Boströmac547a62015-09-17 23:03:57 +020086 RTPSender(bool audio,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000087 Clock* clock,
88 Transport* transport,
sprangebbf8a82015-09-21 15:11:14 -070089 RtpPacketSender* paced_sender,
90 TransportSequenceNumberAllocator* sequence_number_allocator,
sprang5e023eb2015-09-14 06:42:43 -070091 TransportFeedbackObserver* transport_feedback_callback,
andresp@webrtc.org8f151212014-07-10 09:39:23 +000092 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000093 FrameCountObserver* frame_count_observer,
terelius429c3452016-01-21 05:42:04 -080094 SendSideDelayObserver* send_side_delay_observer,
asapersson35151f32016-05-02 23:44:01 -070095 RtcEventLog* event_log,
aluebsa49f1102016-07-08 11:01:59 -070096 SendPacketObserver* send_packet_observer);
asapersson35151f32016-05-02 23:44:01 -070097
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000098 virtual ~RTPSender();
niklase@google.com470e71d2011-07-07 08:21:25 +000099
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000100 void ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 uint16_t ActualSendBitrateKbit() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000104 uint32_t VideoBitrateSent() const;
105 uint32_t FecOverheadRate() const;
106 uint32_t NackOverheadRate() const;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000107
sprange5dd4412016-07-08 09:38:54 -0700108 void SetTargetBitrate(uint32_t bitrate);
aluebsa49f1102016-07-08 11:01:59 -0700109 uint32_t GetTargetBitrate();
110
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000111 // Includes size of RTP and FEC headers.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000112 size_t MaxDataPayloadLength() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
Peter Boström8b79b072016-02-26 16:31:37 +0100114 int32_t RegisterPayload(const char* payload_name,
115 const int8_t payload_type,
116 const uint32_t frequency,
117 const size_t channels,
118 const uint32_t rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000120 int32_t DeRegisterSendPayload(const int8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000122 void SetSendPayloadType(int8_t payload_type);
123
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000124 int8_t SendPayloadType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000126 int SendPayloadFrequency() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000128 void SetSendingStatus(bool enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000130 void SetSendingMediaStatus(bool enabled);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000131 bool SendingMedia() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000133 void GetDataCounters(StreamDataCounters* rtp_stats,
134 StreamDataCounters* rtx_stats) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000136 uint32_t StartTimestamp() const;
137 void SetStartTimestamp(uint32_t timestamp, bool force);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000139 uint32_t GenerateNewSSRC();
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000140 void SetSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000141
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000142 uint16_t SequenceNumber() const override;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000143 void SetSequenceNumber(uint16_t seq);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000145 void SetCsrcs(const std::vector<uint32_t>& csrcs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
danilchap41befce2016-03-30 11:11:51 -0700147 void SetMaxPayloadLength(size_t max_payload_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000149 int32_t SendOutgoingData(FrameType frame_type,
150 int8_t payload_type,
151 uint32_t timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000152 int64_t capture_time_ms,
153 const uint8_t* payload_data,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000154 size_t payload_size,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000155 const RTPFragmentationHeader* fragmentation,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000156 const RTPVideoHeader* rtp_hdr = NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000158 // RTP header extension
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000159 int32_t SetTransmissionTimeOffset(int32_t transmission_time_offset);
160 int32_t SetAbsoluteSendTime(uint32_t absolute_send_time);
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000161 void SetVideoRotation(VideoRotation rotation);
sprang@webrtc.org30933902015-03-17 14:33:12 +0000162 int32_t SetTransportSequenceNumber(uint16_t sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000164 int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
danilchap162abd32015-12-10 02:39:40 -0800165 bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) override;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000166 int32_t DeregisterRtpHeaderExtension(RTPExtensionType type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000167
isheriff6b4b5f32016-06-08 00:24:21 -0700168 size_t RtpHeaderExtensionLength() const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000169
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000170 uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer, bool marker_bit) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000171
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +0000172 uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const;
173 uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const;
174 uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000175 uint8_t BuildVideoRotationExtension(uint8_t* data_buffer) const;
sprang867fb522015-08-03 04:38:41 -0700176 uint8_t BuildTransportSequenceNumberExtension(uint8_t* data_buffer,
177 uint16_t sequence_number) const;
isheriff6b4b5f32016-06-08 00:24:21 -0700178 uint8_t BuildPlayoutDelayExtension(uint8_t* data_buffer,
179 uint16_t min_playout_delay_ms,
180 uint16_t max_playout_delay_ms) const;
sprang867fb522015-08-03 04:38:41 -0700181
182 // Verifies that the specified extension is registered, and that it is
183 // present in rtp packet. If extension is not registered kNotRegistered is
184 // returned. If extension cannot be found in the rtp header, or if it is
185 // malformed, kError is returned. Otherwise *extension_offset is set to the
186 // offset of the extension from the beginning of the rtp packet and kOk is
187 // returned.
188 enum class ExtensionStatus {
189 kNotRegistered,
190 kOk,
191 kError,
192 };
193 ExtensionStatus VerifyExtension(RTPExtensionType extension_type,
194 uint8_t* rtp_packet,
195 size_t rtp_packet_length,
196 const RTPHeader& rtp_header,
197 size_t extension_length_bytes,
198 size_t* extension_offset) const
tommiae695e92016-02-02 08:31:45 -0800199 EXCLUSIVE_LOCKS_REQUIRED(send_critsect_);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000200
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000201 bool UpdateAudioLevel(uint8_t* rtp_packet,
202 size_t rtp_packet_length,
203 const RTPHeader& rtp_header,
204 bool is_voiced,
205 uint8_t dBov) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000206
danilchap162abd32015-12-10 02:39:40 -0800207 bool UpdateVideoRotation(uint8_t* rtp_packet,
208 size_t rtp_packet_length,
209 const RTPHeader& rtp_header,
210 VideoRotation rotation) const override;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000211
philipela1ed0b32016-06-01 06:31:17 -0700212 bool TimeToSendPacket(uint16_t sequence_number,
213 int64_t capture_time_ms,
214 bool retransmission,
215 int probe_cluster_id);
216 size_t TimeToSendPadding(size_t bytes, int probe_cluster_id);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000217
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000218 // NACK.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000219 int SelectiveRetransmissions() const;
220 int SetSelectiveRetransmissions(uint8_t settings);
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000221 void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000222 int64_t avg_rtt);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000223
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000224 void SetStorePacketsStatus(bool enable, uint16_t number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000226 bool StorePackets() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000228 int32_t ReSendPacket(uint16_t packet_id, int64_t min_resend_time = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
aluebsa49f1102016-07-08 11:01:59 -0700230 bool ProcessNACKBitRate(uint32_t now);
231
isheriff6b4b5f32016-06-08 00:24:21 -0700232 // Feedback to decide when to stop sending playout delay.
233 void OnReceivedRtcpReportBlocks(const ReportBlockList& report_blocks);
234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235 // RTX.
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000236 void SetRtxStatus(int mode);
237 int RtxStatus() const;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000238
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000239 uint32_t RtxSsrc() const;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000240 void SetRtxSsrc(uint32_t ssrc);
241
Shao Changbine62202f2015-04-21 20:24:50 +0800242 void SetRtxPayloadType(int payload_type, int associated_payload_type);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000243
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 // Functions wrapping RTPSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000245 int32_t BuildRTPheader(uint8_t* data_buffer,
246 int8_t payload_type,
247 bool marker_bit,
248 uint32_t capture_timestamp,
249 int64_t capture_time_ms,
250 const bool timestamp_provided = true,
251 const bool inc_sequence_number = true) override;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000252
isheriff6b4b5f32016-06-08 00:24:21 -0700253 size_t RtpHeaderLength() const override;
mflodmanfcf54bd2015-04-14 21:28:08 +0200254 uint16_t AllocateSequenceNumber(uint16_t packets_to_send) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000255 size_t MaxPayloadLength() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 // Current timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000258 uint32_t Timestamp() const override;
259 uint32_t SSRC() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000261 int32_t SendToNetwork(uint8_t* data_buffer,
262 size_t payload_length,
263 size_t rtp_header_length,
264 int64_t capture_time_ms,
265 StorageType storage,
sprangebbf8a82015-09-21 15:11:14 -0700266 RtpPacketSender::Priority priority) override;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000267
268 // Audio.
269
270 // Send a DTMF tone using RFC 2833 (4733).
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000271 int32_t SendTelephoneEvent(uint8_t key, uint16_t time_ms, uint8_t level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000272
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000273 // Set audio packet size, used to determine when it's time to send a DTMF
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 // packet in silence (CNG).
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000275 int32_t SetAudioPacketSize(uint16_t packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 // Store the audio level in d_bov for
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000278 // header-extension-for-audio-level-indication.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000279 int32_t SetAudioLevel(uint8_t level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 // Set payload type for Redundant Audio Data RFC 2198.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000282 int32_t SetRED(int8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 // Get payload type for Redundant Audio Data RFC 2198.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000285 int32_t RED(int8_t *payload_type) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000287 RtpVideoCodecTypes VideoCodecType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000289 uint32_t MaxConfiguredBitrateVideo() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 // FEC.
pbosba8c15b2015-07-14 09:36:34 -0700292 void SetGenericFECStatus(bool enable,
293 uint8_t payload_type_red,
294 uint8_t payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
pbosba8c15b2015-07-14 09:36:34 -0700296 void GenericFECStatus(bool* enable,
297 uint8_t* payload_type_red,
298 uint8_t* payload_type_fec) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000300 int32_t SetFecParameters(const FecProtectionParams *delta_params,
301 const FecProtectionParams *key_params);
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
Stefan Holmer586b19b2015-09-18 11:14:31 +0200303 size_t SendPadData(size_t bytes,
304 bool timestamp_provided,
305 uint32_t timestamp,
philipel46948c12016-06-01 04:04:40 -0700306 int64_t capture_time_ms);
stefan@webrtc.orgc4726d02013-12-05 09:16:33 +0000307
philipela1ed0b32016-06-01 06:31:17 -0700308 size_t SendPadData(size_t bytes,
309 bool timestamp_provided,
310 uint32_t timestamp,
311 int64_t capture_time_ms,
312 int probe_cluster_id);
313
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000314 // Called on update of RTP statistics.
315 void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
316 StreamDataCountersCallback* GetRtpStatisticsCallback() const;
317
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000318 uint32_t BitrateSent() const;
319
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000320 void SetRtpState(const RtpState& rtp_state);
321 RtpState GetRtpState() const;
322 void SetRtxRtpState(const RtpState& rtp_state);
323 RtpState GetRtxRtpState() const;
isheriff6b4b5f32016-06-08 00:24:21 -0700324 bool ActivateCVORtpHeaderExtension() override;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000325
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000326 protected:
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000327 int32_t CheckPayloadType(int8_t payload_type, RtpVideoCodecTypes* video_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000329 private:
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000330 // Maps capture time in milliseconds to send-side delay in milliseconds.
331 // Send-side delay is the difference between transmission time and capture
332 // time.
333 typedef std::map<int64_t, int> SendDelayMap;
334
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000335 size_t CreateRtpHeader(uint8_t* header,
336 int8_t payload_type,
337 uint32_t ssrc,
338 bool marker_bit,
339 uint32_t timestamp,
340 uint16_t sequence_number,
341 const std::vector<uint32_t>& csrcs) const;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000342
aluebsa49f1102016-07-08 11:01:59 -0700343 void UpdateNACKBitRate(uint32_t bytes, int64_t now);
344
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000345 bool PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000346 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000347 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000348 bool send_over_rtx,
philipela1ed0b32016-06-01 06:31:17 -0700349 bool is_retransmit,
350 int probe_cluster_id);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000351
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000352 // Return the number of bytes sent. Note that both of these functions may
353 // return a larger value that their argument.
philipela1ed0b32016-06-01 06:31:17 -0700354 size_t TrySendRedundantPayloads(size_t bytes, int probe_cluster_id);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000355
Stefan Holmer586b19b2015-09-18 11:14:31 +0200356 void BuildPaddingPacket(uint8_t* packet,
357 size_t header_length,
358 size_t padding_length);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000359
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000360 void BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000361 uint8_t* buffer_rtx);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000362
stefan1d8a5062015-10-02 03:39:33 -0700363 bool SendPacketToNetwork(const uint8_t* packet,
364 size_t size,
365 const PacketOptions& options);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000366
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000367 void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
asapersson35151f32016-05-02 23:44:01 -0700368 void UpdateOnSendPacket(int packet_id,
369 int64_t capture_time_ms,
370 uint32_t ssrc);
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000371
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000372 // Find the byte position of the RTP extension as indicated by |type| in
373 // |rtp_packet|. Return false if such extension doesn't exist.
374 bool FindHeaderExtensionPosition(RTPExtensionType type,
375 const uint8_t* rtp_packet,
376 size_t rtp_packet_length,
377 const RTPHeader& rtp_header,
378 size_t* position) const;
379
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000380 void UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
381 size_t rtp_packet_length,
382 const RTPHeader& rtp_header,
383 int64_t time_diff_ms) const;
384 void UpdateAbsoluteSendTime(uint8_t* rtp_packet,
385 size_t rtp_packet_length,
386 const RTPHeader& rtp_header,
387 int64_t now_ms) const;
asapersson35151f32016-05-02 23:44:01 -0700388
389 bool UpdateTransportSequenceNumber(uint16_t sequence_number,
390 uint8_t* rtp_packet,
391 size_t rtp_packet_length,
392 const RTPHeader& rtp_header) const;
393
isheriff6b4b5f32016-06-08 00:24:21 -0700394 void UpdatePlayoutDelayLimits(uint8_t* rtp_packet,
395 size_t rtp_packet_length,
396 const RTPHeader& rtp_header,
397 uint16_t min_playout_delay,
398 uint16_t max_playout_delay) const;
399
asapersson35151f32016-05-02 23:44:01 -0700400 bool AllocateTransportSequenceNumber(int* packet_id) const;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000401
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000402 void UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000403 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000404 const RTPHeader& header,
405 bool is_rtx,
406 bool is_retransmit);
407 bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
408
aluebsa49f1102016-07-08 11:01:59 -0700409 class BitrateAggregator {
410 public:
411 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback);
412
413 void OnStatsUpdated() const;
414
415 Bitrate::Observer* total_bitrate_observer();
416 Bitrate::Observer* retransmit_bitrate_observer();
417 void set_ssrc(uint32_t ssrc);
418
419 private:
420 // We assume that these observers are called on the same thread, which is
421 // true for RtpSender as they are called on the Process thread.
422 class BitrateObserver : public Bitrate::Observer {
423 public:
424 explicit BitrateObserver(const BitrateAggregator& aggregator);
425
426 // Implements Bitrate::Observer.
427 void BitrateUpdated(const BitrateStatistics& stats) override;
428 const BitrateStatistics& statistics() const;
429
430 private:
431 BitrateStatistics statistics_;
432 const BitrateAggregator& aggregator_;
433 };
434
435 BitrateStatisticsObserver* const callback_;
436 BitrateObserver total_bitrate_observer_;
437 BitrateObserver retransmit_bitrate_observer_;
438 uint32_t ssrc_;
439 };
440
tommiae695e92016-02-02 08:31:45 -0800441 Clock* const clock_;
442 const int64_t clock_delta_ms_;
danilchap47a740b2015-12-15 00:30:07 -0800443 Random random_ GUARDED_BY(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000444
aluebsa49f1102016-07-08 11:01:59 -0700445 BitrateAggregator bitrates_;
446 Bitrate total_bitrate_sent_;
447
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000448 const bool audio_configured_;
kwiberg84be5112016-04-27 01:19:58 -0700449 const std::unique_ptr<RTPSenderAudio> audio_;
450 const std::unique_ptr<RTPSenderVideo> video_;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000451
sprangebbf8a82015-09-21 15:11:14 -0700452 RtpPacketSender* const paced_sender_;
453 TransportSequenceNumberAllocator* const transport_sequence_number_allocator_;
sprang5e023eb2015-09-14 06:42:43 -0700454 TransportFeedbackObserver* const transport_feedback_observer_;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000455 int64_t last_capture_time_ms_sent_;
tommiae695e92016-02-02 08:31:45 -0800456 rtc::CriticalSection send_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000458 Transport *transport_;
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000459 bool sending_media_ GUARDED_BY(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000461 size_t max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000463 int8_t payload_type_ GUARDED_BY(send_critsect_);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000464 std::map<int8_t, RtpUtility::Payload*> payload_type_map_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000466 RtpHeaderExtensionMap rtp_header_extension_map_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000467 int32_t transmission_time_offset_;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000468 uint32_t absolute_send_time_;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000469 VideoRotation rotation_;
isheriff6b4b5f32016-06-08 00:24:21 -0700470 bool video_rotation_active_;
sprang@webrtc.org30933902015-03-17 14:33:12 +0000471 uint16_t transport_sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
aluebsa49f1102016-07-08 11:01:59 -0700473 // NACK
474 uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
475 size_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
476 Bitrate nack_bitrate_;
477
isheriff6b4b5f32016-06-08 00:24:21 -0700478 // Tracks the current request for playout delay limits from application
479 // and decides whether the current RTP frame should include the playout
480 // delay extension on header.
481 PlayoutDelayOracle playout_delay_oracle_;
482 bool playout_delay_active_ GUARDED_BY(send_critsect_);
483
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000484 RTPPacketHistory packet_history_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000485
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000486 // Statistics
danilchap7c9426c2016-04-14 03:05:31 -0700487 rtc::CriticalSection statistics_crit_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000488 SendDelayMap send_delays_ GUARDED_BY(statistics_crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000489 FrameCounts frame_counts_ GUARDED_BY(statistics_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000490 StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_);
491 StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_);
492 StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_);
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000493 FrameCountObserver* const frame_count_observer_;
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000494 SendSideDelayObserver* const send_side_delay_observer_;
terelius429c3452016-01-21 05:42:04 -0800495 RtcEventLog* const event_log_;
asapersson35151f32016-05-02 23:44:01 -0700496 SendPacketObserver* const send_packet_observer_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000497
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000498 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000499 bool start_timestamp_forced_ GUARDED_BY(send_critsect_);
500 uint32_t start_timestamp_ GUARDED_BY(send_critsect_);
tommiae695e92016-02-02 08:31:45 -0800501 SSRCDatabase* const ssrc_db_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000502 uint32_t remote_ssrc_ GUARDED_BY(send_critsect_);
503 bool sequence_number_forced_ GUARDED_BY(send_critsect_);
504 uint16_t sequence_number_ GUARDED_BY(send_critsect_);
505 uint16_t sequence_number_rtx_ GUARDED_BY(send_critsect_);
506 bool ssrc_forced_ GUARDED_BY(send_critsect_);
507 uint32_t ssrc_ GUARDED_BY(send_critsect_);
508 uint32_t timestamp_ GUARDED_BY(send_critsect_);
509 int64_t capture_time_ms_ GUARDED_BY(send_critsect_);
510 int64_t last_timestamp_time_ms_ GUARDED_BY(send_critsect_);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000511 bool media_has_been_sent_ GUARDED_BY(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000512 bool last_packet_marker_bit_ GUARDED_BY(send_critsect_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000513 std::vector<uint32_t> csrcs_ GUARDED_BY(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000514 int rtx_ GUARDED_BY(send_critsect_);
515 uint32_t ssrc_rtx_ GUARDED_BY(send_critsect_);
Shao Changbine62202f2015-04-21 20:24:50 +0800516 // Mapping rtx_payload_type_map_[associated] = rtx.
517 std::map<int8_t, int8_t> rtx_payload_type_map_ GUARDED_BY(send_critsect_);
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000518
aluebsa49f1102016-07-08 11:01:59 -0700519 // Note: Don't access this variable directly, always go through
520 // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember
521 // that by the time the function returns there is no guarantee
522 // that the target bitrate is still valid.
523 rtc::CriticalSection target_bitrate_critsect_;
524 uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_);
terelius429c3452016-01-21 05:42:04 -0800525
526 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527};
niklase@google.com470e71d2011-07-07 08:21:25 +0000528
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000529} // namespace webrtc
530
531#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_