blob: 542ad8cc03ac0428f5db88429c7c254b2f322c25 [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_
13
pwestin@webrtc.org00741872012-01-19 15:56:10 +000014#include <cassert>
15#include <cmath>
16#include <map>
17
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000018#include "rtp_rtcp_config.h" // misc. defines (e.g. MAX_PACKET_LENGTH)
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000019#include "rtp_rtcp_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#include "common_types.h" // Encryption
21#include "ssrc_database.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022#include "Bitrate.h"
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000023#include "rtp_header_extension.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024#include "video_codec_information.h"
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000025#include "transmission_bucket.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
niklase@google.com470e71d2011-07-07 08:21:25 +000027#define MAX_INIT_RTP_SEQ_NUMBER 32767 // 2^15 -1
28
29namespace webrtc {
30class CriticalSectionWrapper;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000031class RTPPacketHistory;
niklase@google.com470e71d2011-07-07 08:21:25 +000032class RTPSenderAudio;
33class RTPSenderVideo;
34
35class RTPSenderInterface
36{
37public:
38 RTPSenderInterface() {}
39 virtual ~RTPSenderInterface() {}
40
41 virtual WebRtc_UWord32 SSRC() const = 0;
42 virtual WebRtc_UWord32 Timestamp() const = 0;
43
44 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
45 const WebRtc_Word8 payloadType,
46 const bool markerBit,
47 const WebRtc_UWord32 captureTimeStamp,
48 const bool timeStampProvided = true,
49 const bool incSequenceNumber = true) = 0;
50
51 virtual WebRtc_UWord16 RTPHeaderLength() const = 0;
52 virtual WebRtc_UWord16 IncrementSequenceNumber() = 0;
53 virtual WebRtc_UWord16 SequenceNumber() const = 0;
54 virtual WebRtc_UWord16 MaxPayloadLength() const = 0;
henrik.lundin@webrtc.orgf15fbc32011-11-08 08:23:47 +000055 virtual WebRtc_UWord16 MaxDataPayloadLength() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000056 virtual WebRtc_UWord16 PacketOverHead() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000057 virtual WebRtc_UWord16 ActualSendBitrateKbit() const = 0;
58
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +000059 virtual WebRtc_Word32 SendToNetwork(WebRtc_UWord8* data_buffer,
60 const WebRtc_UWord16 payload_length,
61 const WebRtc_UWord16 rtp_header_length,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +000062 int64_t capture_time_ms,
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +000063 const StorageType storage) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000064};
65
66class RTPSender : public Bitrate, public RTPSenderInterface
67{
68public:
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000069 RTPSender(const WebRtc_Word32 id, const bool audio, RtpRtcpClock* clock);
niklase@google.com470e71d2011-07-07 08:21:25 +000070 virtual ~RTPSender();
71
niklase@google.com470e71d2011-07-07 08:21:25 +000072 void ProcessBitrate();
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000073 void ProcessSendToNetwork();
niklase@google.com470e71d2011-07-07 08:21:25 +000074
niklase@google.com470e71d2011-07-07 08:21:25 +000075 WebRtc_UWord16 ActualSendBitrateKbit() const;
76
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +000077 WebRtc_UWord32 VideoBitrateSent() const;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +000078 WebRtc_UWord32 FecOverheadRate() const;
79 WebRtc_UWord32 NackOverheadRate() const;
80
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000081 void SetTargetSendBitrate(const WebRtc_UWord32 bits);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
83 WebRtc_UWord16 MaxDataPayloadLength() const; // with RTP and FEC headers
84
85 // callback
86 WebRtc_Word32 RegisterSendTransport(Transport* outgoingTransport);
87
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000088 WebRtc_Word32 RegisterPayload(
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +000089 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000090 const WebRtc_Word8 payloadType,
91 const WebRtc_UWord32 frequency,
92 const WebRtc_UWord8 channels,
93 const WebRtc_UWord32 rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
95 WebRtc_Word32 DeRegisterSendPayload(const WebRtc_Word8 payloadType);
96
97 WebRtc_Word8 SendPayloadType() const;
98
99 int SendPayloadFrequency() const;
100
101 void SetSendingStatus(const bool enabled);
102
103 void SetSendingMediaStatus(const bool enabled);
104 bool SendingMedia() const;
105
106 // number of sent RTP packets
107 WebRtc_UWord32 Packets() const;
108
109 // number of sent RTP bytes
110 WebRtc_UWord32 Bytes() const;
111
112 WebRtc_Word32 ResetDataCounters();
113
114 WebRtc_UWord32 StartTimestamp() const;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000115 WebRtc_Word32 SetStartTimestamp(const WebRtc_UWord32 timestamp,
116 const bool force = false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
118 WebRtc_UWord32 GenerateNewSSRC();
119 WebRtc_Word32 SetSSRC( const WebRtc_UWord32 ssrc);
120
121 WebRtc_UWord16 SequenceNumber() const;
122 WebRtc_Word32 SetSequenceNumber( WebRtc_UWord16 seq);
123
124 WebRtc_Word32 CSRCs(WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize]) const;
125
126 WebRtc_Word32 SetCSRCStatus(const bool include);
127
128 WebRtc_Word32 SetCSRCs(const WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize],
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000129 const WebRtc_UWord8 arrLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
131 WebRtc_Word32 SetMaxPayloadLength(const WebRtc_UWord16 length,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000132 const WebRtc_UWord16 packetOverHead);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000134 WebRtc_Word32 SendOutgoingData(const FrameType frameType,
135 const WebRtc_Word8 payloadType,
136 const WebRtc_UWord32 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000137 int64_t capture_time_ms,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000138 const WebRtc_UWord8* payloadData,
139 const WebRtc_UWord32 payloadSize,
140 const RTPFragmentationHeader* fragmentation,
141 VideoCodecInformation* codecInfo = NULL,
142 const RTPVideoTypeHeader* rtpTypeHdr = NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000144 WebRtc_Word32 SendPadData(WebRtc_Word8 payload_type,
145 WebRtc_UWord32 capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000146 int64_t capture_time_ms,
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000147 WebRtc_Word32 bytes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 /*
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000149 * RTP header extension
150 */
151 WebRtc_Word32 SetTransmissionTimeOffset(
152 const WebRtc_Word32 transmissionTimeOffset);
153
154 WebRtc_Word32 RegisterRtpHeaderExtension(const RTPExtensionType type,
155 const WebRtc_UWord8 id);
156
157 WebRtc_Word32 DeregisterRtpHeaderExtension(const RTPExtensionType type);
158
159 WebRtc_UWord16 RtpHeaderExtensionTotalLength() const;
160
161 WebRtc_UWord16 BuildRTPHeaderExtension(WebRtc_UWord8* dataBuffer) const;
162
163 WebRtc_UWord8 BuildTransmissionTimeOffsetExtension(
164 WebRtc_UWord8* dataBuffer) const;
165
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000166 bool UpdateTransmissionTimeOffset(WebRtc_UWord8* rtp_packet,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000167 const WebRtc_UWord16 rtp_packet_length,
168 const WebRtcRTPHeader& rtp_header,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000169 const WebRtc_Word64 time_diff_ms) const;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000170
171 void SetTransmissionSmoothingStatus(const bool enable);
172
173 bool TransmissionSmoothingStatus() const;
174
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000175 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 * NACK
177 */
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000178 int SelectiveRetransmissions() const;
179 int SetSelectiveRetransmissions(uint8_t settings);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 void OnReceivedNACK(const WebRtc_UWord16 nackSequenceNumbersLength,
181 const WebRtc_UWord16* nackSequenceNumbers,
182 const WebRtc_UWord16 avgRTT);
183
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000184 WebRtc_Word32 SetStorePacketsStatus(const bool enable,
185 const WebRtc_UWord16 numberToStore);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
187 bool StorePackets() const;
188
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000189 WebRtc_Word32 ReSendPacket(WebRtc_UWord16 packet_id,
190 WebRtc_UWord32 min_resend_time = 0);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000191
192 WebRtc_Word32 ReSendToNetwork(const WebRtc_UWord8* packet,
193 const WebRtc_UWord32 size);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
195 bool ProcessNACKBitRate(const WebRtc_UWord32 now);
196
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 /*
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000198 * RTX
199 */
200 void SetRTXStatus(const bool enable,
201 const bool setSSRC,
202 const WebRtc_UWord32 SSRC);
203
204 void RTXStatus(bool* enable, WebRtc_UWord32* SSRC) const;
205
206 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 * Functions wrapping RTPSenderInterface
208 */
209 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
210 const WebRtc_Word8 payloadType,
211 const bool markerBit,
212 const WebRtc_UWord32 captureTimeStamp,
213 const bool timeStampProvided = true,
214 const bool incSequenceNumber = true);
215
216 virtual WebRtc_UWord16 RTPHeaderLength() const ;
217 virtual WebRtc_UWord16 IncrementSequenceNumber();
218 virtual WebRtc_UWord16 MaxPayloadLength() const;
219 virtual WebRtc_UWord16 PacketOverHead() const;
220
221 // current timestamp
222 virtual WebRtc_UWord32 Timestamp() const;
223 virtual WebRtc_UWord32 SSRC() const;
224
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000225 virtual WebRtc_Word32 SendToNetwork(WebRtc_UWord8* data_buffer,
226 const WebRtc_UWord16 payload_length,
227 const WebRtc_UWord16 rtp_header_length,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000228 int64_t capture_time_ms,
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000229 const StorageType storage);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
231 /*
232 * Audio
233 */
234 WebRtc_Word32 RegisterAudioCallback(RtpAudioFeedback* messagesCallback);
235
236 // Send a DTMF tone using RFC 2833 (4733)
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000237 WebRtc_Word32 SendTelephoneEvent(const WebRtc_UWord8 key,
238 const WebRtc_UWord16 time_ms,
239 const WebRtc_UWord8 level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
241 bool SendTelephoneEventActive(WebRtc_Word8& telephoneEvent) const;
242
243 // set audio packet size, used to determine when it's time to send a DTMF packet in silence (CNG)
244 WebRtc_Word32 SetAudioPacketSize(const WebRtc_UWord16 packetSizeSamples);
245
246 // Set status and ID for header-extension-for-audio-level-indication.
247 WebRtc_Word32 SetAudioLevelIndicationStatus(const bool enable,
248 const WebRtc_UWord8 ID);
249
250 // Get status and ID for header-extension-for-audio-level-indication.
251 WebRtc_Word32 AudioLevelIndicationStatus(bool& enable,
252 WebRtc_UWord8& ID) const;
253
254 // Store the audio level in dBov for header-extension-for-audio-level-indication.
255 WebRtc_Word32 SetAudioLevel(const WebRtc_UWord8 level_dBov);
256
257 // Set payload type for Redundant Audio Data RFC 2198
258 WebRtc_Word32 SetRED(const WebRtc_Word8 payloadType);
259
260 // Get payload type for Redundant Audio Data RFC 2198
261 WebRtc_Word32 RED(WebRtc_Word8& payloadType) const;
262
263 /*
264 * Video
265 */
266 VideoCodecInformation* CodecInformationVideo();
267
268 RtpVideoCodecTypes VideoCodecType() const;
269
270 WebRtc_UWord32 MaxConfiguredBitrateVideo() const;
271
272 WebRtc_Word32 SendRTPIntraRequest();
273
274 // FEC
275 WebRtc_Word32 SetGenericFECStatus(const bool enable,
276 const WebRtc_UWord8 payloadTypeRED,
277 const WebRtc_UWord8 payloadTypeFEC);
278
279 WebRtc_Word32 GenericFECStatus(bool& enable,
280 WebRtc_UWord8& payloadTypeRED,
281 WebRtc_UWord8& payloadTypeFEC) const;
282
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000283 WebRtc_Word32 SetFecParameters(
284 const FecProtectionParams* delta_params,
285 const FecProtectionParams* key_params);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
287protected:
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000288 WebRtc_Word32 CheckPayloadType(const WebRtc_Word8 payloadType,
289 RtpVideoCodecTypes& videoType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
291private:
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000292 void UpdateNACKBitRate(const WebRtc_UWord32 bytes,
293 const WebRtc_UWord32 now);
294
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000295 WebRtc_Word32 SendPaddingAccordingToBitrate(
296 WebRtc_Word8 payload_type,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000297 WebRtc_UWord32 capture_timestamp,
298 int64_t capture_time_ms);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000299
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000300 WebRtc_Word32 _id;
301 const bool _audioConfigured;
302 RTPSenderAudio* _audio;
303 RTPSenderVideo* _video;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000304
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000305 CriticalSectionWrapper* _sendCritsect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000307 CriticalSectionWrapper* _transportCritsect;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000308 Transport* _transport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
310 bool _sendingMedia;
311
312 WebRtc_UWord16 _maxPayloadLength;
313 WebRtc_UWord16 _targetSendBitrate;
314 WebRtc_UWord16 _packetOverHead;
315
316 WebRtc_Word8 _payloadType;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000317 std::map<WebRtc_Word8, ModuleRTPUtility::Payload*> _payloadTypeMap;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000319 RtpHeaderExtensionMap _rtpHeaderExtensionMap;
320 WebRtc_Word32 _transmissionTimeOffset;
321
niklase@google.com470e71d2011-07-07 08:21:25 +0000322 // NACK
323 WebRtc_UWord32 _nackByteCountTimes[NACK_BYTECOUNT_SIZE];
324 WebRtc_Word32 _nackByteCount[NACK_BYTECOUNT_SIZE];
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000325 Bitrate _nackBitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000327 RTPPacketHistory* _packetHistory;
328 TransmissionBucket _sendBucket;
pwestin@webrtc.org18530052012-07-03 10:41:54 +0000329 WebRtc_Word64 _timeLastSendToNetworkUpdate;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000330 bool _transmissionSmoothing;
331
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 // statistics
333 WebRtc_UWord32 _packetsSent;
334 WebRtc_UWord32 _payloadBytesSent;
335
336 // RTP variables
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000337 bool _startTimeStampForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338 WebRtc_UWord32 _startTimeStamp;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000339 SSRCDatabase& _ssrcDB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000340 WebRtc_UWord32 _remoteSSRC;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000341 bool _sequenceNumberForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000342 WebRtc_UWord16 _sequenceNumber;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000343 WebRtc_UWord16 _sequenceNumberRTX;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000344 bool _ssrcForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345 WebRtc_UWord32 _ssrc;
346 WebRtc_UWord32 _timeStamp;
347 WebRtc_UWord8 _CSRCs;
348 WebRtc_UWord32 _CSRC[kRtpCsrcSize];
349 bool _includeCSRCs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000350 bool _RTX;
351 WebRtc_UWord32 _ssrcRTX;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352};
353} // namespace webrtc
354
355#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_