blob: 49d98729ee27b356c3c644e4bc1d6f6a3ef813a5 [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>
danilchapb8b6fbb2015-12-10 05:05:27 -080016#include <utility>
17#include <vector>
pwestin@webrtc.org00741872012-01-19 15:56:10 +000018
tommiae695e92016-02-02 08:31:45 -080019#include "webrtc/base/criticalsection.h"
danilchap47a740b2015-12-15 00:30:07 -080020#include "webrtc/base/random.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000021#include "webrtc/base/thread_annotations.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000022#include "webrtc/common_types.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +000024#include "webrtc/modules/rtp_rtcp/source/bitrate.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000025#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000026#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000027#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
mflodmanfcf54bd2015-04-14 21:28:08 +020028#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000029#include "webrtc/modules/rtp_rtcp/source/ssrc_database.h"
pbos2d566682015-09-28 09:59:31 -070030#include "webrtc/transport.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
niklase@google.com470e71d2011-07-07 08:21:25 +000032namespace webrtc {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000033
niklase@google.com470e71d2011-07-07 08:21:25 +000034class RTPSenderAudio;
35class RTPSenderVideo;
terelius429c3452016-01-21 05:42:04 -080036class RtcEventLog;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000038class RTPSenderInterface {
39 public:
40 RTPSenderInterface() {}
41 virtual ~RTPSenderInterface() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000042
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -070043 enum CVOMode {
44 kCVONone,
45 kCVOInactive, // CVO rtp header extension is registered but haven't
46 // received any frame with rotation pending.
47 kCVOActivated, // CVO rtp header extension will be present in the rtp
48 // packets.
49 };
50
pbos@webrtc.org2f446732013-04-08 11:08:41 +000051 virtual uint32_t SSRC() const = 0;
52 virtual uint32_t Timestamp() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000054 virtual int32_t BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000055 int8_t payload_type,
56 bool marker_bit,
57 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000058 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000059 bool timestamp_provided = true,
60 bool inc_sequence_number = true) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000061
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000062 virtual size_t RTPHeaderLength() const = 0;
mflodmanfcf54bd2015-04-14 21:28:08 +020063 // Returns the next sequence number to use for a packet and allocates
64 // 'packets_to_send' number of sequence numbers. It's important all allocated
65 // sequence numbers are used in sequence to avoid perceived packet loss.
66 virtual uint16_t AllocateSequenceNumber(uint16_t packets_to_send) = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000067 virtual uint16_t SequenceNumber() const = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000068 virtual size_t MaxPayloadLength() const = 0;
69 virtual size_t MaxDataPayloadLength() const = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000070 virtual uint16_t PacketOverHead() const = 0;
71 virtual uint16_t ActualSendBitrateKbit() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
sprangebbf8a82015-09-21 15:11:14 -070073 virtual int32_t SendToNetwork(uint8_t* data_buffer,
74 size_t payload_length,
75 size_t rtp_header_length,
76 int64_t capture_time_ms,
77 StorageType storage,
78 RtpPacketSender::Priority priority) = 0;
guoweis@webrtc.org45362892015-03-04 22:55:15 +000079
80 virtual bool UpdateVideoRotation(uint8_t* rtp_packet,
81 size_t rtp_packet_length,
82 const RTPHeader& rtp_header,
83 VideoRotation rotation) const = 0;
84 virtual bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) = 0;
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -070085 virtual CVOMode ActivateCVORtpHeaderExtension() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000086};
87
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000088class RTPSender : public RTPSenderInterface {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000089 public:
Peter Boströmac547a62015-09-17 23:03:57 +020090 RTPSender(bool audio,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000091 Clock* clock,
92 Transport* transport,
93 RtpAudioFeedback* audio_feedback,
sprangebbf8a82015-09-21 15:11:14 -070094 RtpPacketSender* paced_sender,
95 TransportSequenceNumberAllocator* sequence_number_allocator,
sprang5e023eb2015-09-14 06:42:43 -070096 TransportFeedbackObserver* transport_feedback_callback,
andresp@webrtc.org8f151212014-07-10 09:39:23 +000097 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000098 FrameCountObserver* frame_count_observer,
terelius429c3452016-01-21 05:42:04 -080099 SendSideDelayObserver* send_side_delay_observer,
100 RtcEventLog* event_log);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000101 virtual ~RTPSender();
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000103 void ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 uint16_t ActualSendBitrateKbit() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107 uint32_t VideoBitrateSent() const;
108 uint32_t FecOverheadRate() const;
109 uint32_t NackOverheadRate() const;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000110
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000111 void SetTargetBitrate(uint32_t bitrate);
112 uint32_t GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000114 // Includes size of RTP and FEC headers.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 size_t MaxDataPayloadLength() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000117 int32_t RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000118 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000119 const int8_t payload_type, const uint32_t frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800120 const size_t channels, const uint32_t rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122 int32_t DeRegisterSendPayload(const int8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000124 void SetSendPayloadType(int8_t payload_type);
125
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000126 int8_t SendPayloadType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000128 int SendPayloadFrequency() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000130 void SetSendingStatus(bool enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000132 void SetSendingMediaStatus(bool enabled);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000133 bool SendingMedia() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +0000135 void GetDataCounters(StreamDataCounters* rtp_stats,
136 StreamDataCounters* rtx_stats) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000138 uint32_t StartTimestamp() const;
139 void SetStartTimestamp(uint32_t timestamp, bool force);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000141 uint32_t GenerateNewSSRC();
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000142 void SetSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000144 uint16_t SequenceNumber() const override;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145 void SetSequenceNumber(uint16_t seq);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000147 void SetCsrcs(const std::vector<uint32_t>& csrcs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000149 int32_t SetMaxPayloadLength(size_t length, uint16_t packet_over_head);
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000151 int32_t SendOutgoingData(FrameType frame_type,
152 int8_t payload_type,
153 uint32_t timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000154 int64_t capture_time_ms,
155 const uint8_t* payload_data,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000156 size_t payload_size,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000157 const RTPFragmentationHeader* fragmentation,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000158 const RTPVideoHeader* rtp_hdr = NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 // RTP header extension
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000161 int32_t SetTransmissionTimeOffset(int32_t transmission_time_offset);
162 int32_t SetAbsoluteSendTime(uint32_t absolute_send_time);
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000163 void SetVideoRotation(VideoRotation rotation);
sprang@webrtc.org30933902015-03-17 14:33:12 +0000164 int32_t SetTransportSequenceNumber(uint16_t sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000166 int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
danilchap162abd32015-12-10 02:39:40 -0800167 bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) override;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000168 int32_t DeregisterRtpHeaderExtension(RTPExtensionType type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000169
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000170 size_t RtpHeaderExtensionTotalLength() const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000171
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000172 uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer, bool marker_bit) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000173
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +0000174 uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const;
175 uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const;
176 uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000177 uint8_t BuildVideoRotationExtension(uint8_t* data_buffer) const;
sprang867fb522015-08-03 04:38:41 -0700178 uint8_t BuildTransportSequenceNumberExtension(uint8_t* data_buffer,
179 uint16_t sequence_number) const;
180
181 // Verifies that the specified extension is registered, and that it is
182 // present in rtp packet. If extension is not registered kNotRegistered is
183 // returned. If extension cannot be found in the rtp header, or if it is
184 // malformed, kError is returned. Otherwise *extension_offset is set to the
185 // offset of the extension from the beginning of the rtp packet and kOk is
186 // returned.
187 enum class ExtensionStatus {
188 kNotRegistered,
189 kOk,
190 kError,
191 };
192 ExtensionStatus VerifyExtension(RTPExtensionType extension_type,
193 uint8_t* rtp_packet,
194 size_t rtp_packet_length,
195 const RTPHeader& rtp_header,
196 size_t extension_length_bytes,
197 size_t* extension_offset) const
tommiae695e92016-02-02 08:31:45 -0800198 EXCLUSIVE_LOCKS_REQUIRED(send_critsect_);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000199
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000200 bool UpdateAudioLevel(uint8_t* rtp_packet,
201 size_t rtp_packet_length,
202 const RTPHeader& rtp_header,
203 bool is_voiced,
204 uint8_t dBov) const;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000205
danilchap162abd32015-12-10 02:39:40 -0800206 bool UpdateVideoRotation(uint8_t* rtp_packet,
207 size_t rtp_packet_length,
208 const RTPHeader& rtp_header,
209 VideoRotation rotation) const override;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000210
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000211 bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms,
212 bool retransmission);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000213 size_t TimeToSendPadding(size_t bytes);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000214
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 // NACK.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000216 int SelectiveRetransmissions() const;
217 int SetSelectiveRetransmissions(uint8_t settings);
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000218 void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000219 int64_t avg_rtt);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000220
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000221 void SetStorePacketsStatus(bool enable, uint16_t number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000223 bool StorePackets() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000225 int32_t ReSendPacket(uint16_t packet_id, int64_t min_resend_time = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000226
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000227 bool ProcessNACKBitRate(uint32_t now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 // RTX.
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000230 void SetRtxStatus(int mode);
231 int RtxStatus() const;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000232
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000233 uint32_t RtxSsrc() const;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000234 void SetRtxSsrc(uint32_t ssrc);
235
Shao Changbine62202f2015-04-21 20:24:50 +0800236 void SetRtxPayloadType(int payload_type, int associated_payload_type);
237 std::pair<int, int> RtxPayloadType() const;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000238
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 // Functions wrapping RTPSenderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000240 int32_t BuildRTPheader(uint8_t* data_buffer,
241 int8_t payload_type,
242 bool marker_bit,
243 uint32_t capture_timestamp,
244 int64_t capture_time_ms,
245 const bool timestamp_provided = true,
246 const bool inc_sequence_number = true) override;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000247
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000248 size_t RTPHeaderLength() const override;
mflodmanfcf54bd2015-04-14 21:28:08 +0200249 uint16_t AllocateSequenceNumber(uint16_t packets_to_send) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000250 size_t MaxPayloadLength() const override;
251 uint16_t PacketOverHead() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000253 // Current timestamp.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000254 uint32_t Timestamp() const override;
255 uint32_t SSRC() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000257 int32_t SendToNetwork(uint8_t* data_buffer,
258 size_t payload_length,
259 size_t rtp_header_length,
260 int64_t capture_time_ms,
261 StorageType storage,
sprangebbf8a82015-09-21 15:11:14 -0700262 RtpPacketSender::Priority priority) override;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263
264 // Audio.
265
266 // Send a DTMF tone using RFC 2833 (4733).
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000267 int32_t SendTelephoneEvent(uint8_t key, uint16_t time_ms, uint8_t level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000269 // Set audio packet size, used to determine when it's time to send a DTMF
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 // packet in silence (CNG).
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000271 int32_t SetAudioPacketSize(uint16_t packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000272
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 // Store the audio level in d_bov for
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000274 // header-extension-for-audio-level-indication.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000275 int32_t SetAudioLevel(uint8_t level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 // Set payload type for Redundant Audio Data RFC 2198.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000278 int32_t SetRED(int8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000280 // Get payload type for Redundant Audio Data RFC 2198.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000281 int32_t RED(int8_t *payload_type) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000283 RtpVideoCodecTypes VideoCodecType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000285 uint32_t MaxConfiguredBitrateVideo() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 // FEC.
pbosba8c15b2015-07-14 09:36:34 -0700288 void SetGenericFECStatus(bool enable,
289 uint8_t payload_type_red,
290 uint8_t payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
pbosba8c15b2015-07-14 09:36:34 -0700292 void GenericFECStatus(bool* enable,
293 uint8_t* payload_type_red,
294 uint8_t* payload_type_fec) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000296 int32_t SetFecParameters(const FecProtectionParams *delta_params,
297 const FecProtectionParams *key_params);
niklase@google.com470e71d2011-07-07 08:21:25 +0000298
Stefan Holmer586b19b2015-09-18 11:14:31 +0200299 size_t SendPadData(size_t bytes,
300 bool timestamp_provided,
301 uint32_t timestamp,
302 int64_t capture_time_ms);
stefan@webrtc.orgc4726d02013-12-05 09:16:33 +0000303
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000304 // Called on update of RTP statistics.
305 void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback);
306 StreamDataCountersCallback* GetRtpStatisticsCallback() const;
307
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000308 uint32_t BitrateSent() const;
309
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000310 void SetRtpState(const RtpState& rtp_state);
311 RtpState GetRtpState() const;
312 void SetRtxRtpState(const RtpState& rtp_state);
313 RtpState GetRtxRtpState() const;
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700314 CVOMode ActivateCVORtpHeaderExtension() override;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000315
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000316 protected:
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000317 int32_t CheckPayloadType(int8_t payload_type, RtpVideoCodecTypes* video_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000319 private:
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000320 // Maps capture time in milliseconds to send-side delay in milliseconds.
321 // Send-side delay is the difference between transmission time and capture
322 // time.
323 typedef std::map<int64_t, int> SendDelayMap;
324
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000325 size_t CreateRtpHeader(uint8_t* header,
326 int8_t payload_type,
327 uint32_t ssrc,
328 bool marker_bit,
329 uint32_t timestamp,
330 uint16_t sequence_number,
331 const std::vector<uint32_t>& csrcs) const;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000332
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000333 void UpdateNACKBitRate(uint32_t bytes, int64_t now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000334
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000335 bool PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000336 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000337 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000338 bool send_over_rtx,
339 bool is_retransmit);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000340
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000341 // Return the number of bytes sent. Note that both of these functions may
342 // return a larger value that their argument.
343 size_t TrySendRedundantPayloads(size_t bytes);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000344
Stefan Holmer586b19b2015-09-18 11:14:31 +0200345 void BuildPaddingPacket(uint8_t* packet,
346 size_t header_length,
347 size_t padding_length);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000348
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000349 void BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000350 uint8_t* buffer_rtx);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000351
stefan1d8a5062015-10-02 03:39:33 -0700352 bool SendPacketToNetwork(const uint8_t* packet,
353 size_t size,
354 const PacketOptions& options);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000355
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000356 void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
357
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000358 // Find the byte position of the RTP extension as indicated by |type| in
359 // |rtp_packet|. Return false if such extension doesn't exist.
360 bool FindHeaderExtensionPosition(RTPExtensionType type,
361 const uint8_t* rtp_packet,
362 size_t rtp_packet_length,
363 const RTPHeader& rtp_header,
364 size_t* position) const;
365
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000366 void UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
367 size_t rtp_packet_length,
368 const RTPHeader& rtp_header,
369 int64_t time_diff_ms) const;
370 void UpdateAbsoluteSendTime(uint8_t* rtp_packet,
371 size_t rtp_packet_length,
372 const RTPHeader& rtp_header,
373 int64_t now_ms) const;
sprang867fb522015-08-03 04:38:41 -0700374 // Update the transport sequence number of the packet using a new sequence
sprangebbf8a82015-09-21 15:11:14 -0700375 // number allocated by SequenceNumberAllocator. Returns the assigned sequence
376 // number, or 0 if extension could not be updated.
sprang867fb522015-08-03 04:38:41 -0700377 uint16_t UpdateTransportSequenceNumber(uint8_t* rtp_packet,
378 size_t rtp_packet_length,
379 const RTPHeader& rtp_header) const;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000380
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000381 void UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000382 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000383 const RTPHeader& header,
384 bool is_rtx,
385 bool is_retransmit);
386 bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const;
387
tommiae695e92016-02-02 08:31:45 -0800388 class BitrateAggregator {
389 public:
390 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback);
391
392 void OnStatsUpdated() const;
393
394 Bitrate::Observer* total_bitrate_observer();
395 Bitrate::Observer* retransmit_bitrate_observer();
396 void set_ssrc(uint32_t ssrc);
397
398 private:
399 // We assume that these observers are called on the same thread, which is
400 // true for RtpSender as they are called on the Process thread.
401 class BitrateObserver : public Bitrate::Observer {
402 public:
403 explicit BitrateObserver(const BitrateAggregator& aggregator);
404
405 // Implements Bitrate::Observer.
406 void BitrateUpdated(const BitrateStatistics& stats) override;
407 const BitrateStatistics& statistics() const;
408
409 private:
410 BitrateStatistics statistics_;
411 const BitrateAggregator& aggregator_;
412 };
413
414 BitrateStatisticsObserver* const callback_;
415 BitrateObserver total_bitrate_observer_;
416 BitrateObserver retransmit_bitrate_observer_;
417 uint32_t ssrc_;
418 };
419
420 Clock* const clock_;
421 const int64_t clock_delta_ms_;
danilchap47a740b2015-12-15 00:30:07 -0800422 Random random_ GUARDED_BY(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000423
tommiae695e92016-02-02 08:31:45 -0800424 BitrateAggregator bitrates_;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000425 Bitrate total_bitrate_sent_;
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000426
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000427 const bool audio_configured_;
tommiae695e92016-02-02 08:31:45 -0800428 const rtc::scoped_ptr<RTPSenderAudio> audio_;
429 const rtc::scoped_ptr<RTPSenderVideo> video_;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000430
sprangebbf8a82015-09-21 15:11:14 -0700431 RtpPacketSender* const paced_sender_;
432 TransportSequenceNumberAllocator* const transport_sequence_number_allocator_;
sprang5e023eb2015-09-14 06:42:43 -0700433 TransportFeedbackObserver* const transport_feedback_observer_;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000434 int64_t last_capture_time_ms_sent_;
tommiae695e92016-02-02 08:31:45 -0800435 rtc::CriticalSection send_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000437 Transport *transport_;
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000438 bool sending_media_ GUARDED_BY(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000440 size_t max_payload_length_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000441 uint16_t packet_over_head_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000443 int8_t payload_type_ GUARDED_BY(send_critsect_);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000444 std::map<int8_t, RtpUtility::Payload*> payload_type_map_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000446 RtpHeaderExtensionMap rtp_header_extension_map_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000447 int32_t transmission_time_offset_;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000448 uint32_t absolute_send_time_;
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000449 VideoRotation rotation_;
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700450 CVOMode cvo_mode_;
sprang@webrtc.org30933902015-03-17 14:33:12 +0000451 uint16_t transport_sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000453 // NACK
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000454 uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000455 size_t nack_byte_count_[NACK_BYTECOUNT_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000456 Bitrate nack_bitrate_;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000457
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000458 RTPPacketHistory packet_history_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000460 // Statistics
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000461 rtc::scoped_ptr<CriticalSectionWrapper> statistics_crit_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000462 SendDelayMap send_delays_ GUARDED_BY(statistics_crit_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000463 FrameCounts frame_counts_ GUARDED_BY(statistics_crit_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000464 StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_);
465 StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_);
466 StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_);
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000467 FrameCountObserver* const frame_count_observer_;
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000468 SendSideDelayObserver* const send_side_delay_observer_;
terelius429c3452016-01-21 05:42:04 -0800469 RtcEventLog* const event_log_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000470
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000471 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000472 bool start_timestamp_forced_ GUARDED_BY(send_critsect_);
473 uint32_t start_timestamp_ GUARDED_BY(send_critsect_);
tommiae695e92016-02-02 08:31:45 -0800474 SSRCDatabase* const ssrc_db_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000475 uint32_t remote_ssrc_ GUARDED_BY(send_critsect_);
476 bool sequence_number_forced_ GUARDED_BY(send_critsect_);
477 uint16_t sequence_number_ GUARDED_BY(send_critsect_);
478 uint16_t sequence_number_rtx_ GUARDED_BY(send_critsect_);
479 bool ssrc_forced_ GUARDED_BY(send_critsect_);
480 uint32_t ssrc_ GUARDED_BY(send_critsect_);
481 uint32_t timestamp_ GUARDED_BY(send_critsect_);
482 int64_t capture_time_ms_ GUARDED_BY(send_critsect_);
483 int64_t last_timestamp_time_ms_ GUARDED_BY(send_critsect_);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000484 bool media_has_been_sent_ GUARDED_BY(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000485 bool last_packet_marker_bit_ GUARDED_BY(send_critsect_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000486 std::vector<uint32_t> csrcs_ GUARDED_BY(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000487 int rtx_ GUARDED_BY(send_critsect_);
488 uint32_t ssrc_rtx_ GUARDED_BY(send_critsect_);
Shao Changbine62202f2015-04-21 20:24:50 +0800489 // TODO(changbin): Remove rtx_payload_type_ once interop with old clients that
490 // only understand one RTX PT is no longer needed.
491 int rtx_payload_type_ GUARDED_BY(send_critsect_);
492 // Mapping rtx_payload_type_map_[associated] = rtx.
493 std::map<int8_t, int8_t> rtx_payload_type_map_ GUARDED_BY(send_critsect_);
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000494
495 // Note: Don't access this variable directly, always go through
496 // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember
497 // that by the time the function returns there is no guarantee
498 // that the target bitrate is still valid.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000499 rtc::scoped_ptr<CriticalSectionWrapper> target_bitrate_critsect_;
stefan@webrtc.orgaa0e56e2014-06-26 11:44:49 +0000500 uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_);
terelius429c3452016-01-21 05:42:04 -0800501
502 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503};
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000505} // namespace webrtc
506
507#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_