blob: da2e67bfb38eb8fdd82e6347d830314fd2085bd3 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_
13
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000014#include "rtp_rtcp_config.h" // misc. defines (e.g. MAX_PACKET_LENGTH)
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000015#include "rtp_rtcp_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016#include "common_types.h" // Encryption
17#include "ssrc_database.h"
18#include "list_wrapper.h"
19#include "map_wrapper.h"
20#include "Bitrate.h"
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +000021#include "rtp_header_extension.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022#include "video_codec_information.h"
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000023#include "transmission_bucket.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25#include <cassert>
26#include <cmath>
27
28#define MAX_INIT_RTP_SEQ_NUMBER 32767 // 2^15 -1
29
30namespace webrtc {
31class CriticalSectionWrapper;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000032class RTPPacketHistory;
niklase@google.com470e71d2011-07-07 08:21:25 +000033class RTPSenderAudio;
34class RTPSenderVideo;
35
36class RTPSenderInterface
37{
38public:
39 RTPSenderInterface() {}
40 virtual ~RTPSenderInterface() {}
41
42 virtual WebRtc_UWord32 SSRC() const = 0;
43 virtual WebRtc_UWord32 Timestamp() const = 0;
44
45 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
46 const WebRtc_Word8 payloadType,
47 const bool markerBit,
48 const WebRtc_UWord32 captureTimeStamp,
49 const bool timeStampProvided = true,
50 const bool incSequenceNumber = true) = 0;
51
52 virtual WebRtc_UWord16 RTPHeaderLength() const = 0;
53 virtual WebRtc_UWord16 IncrementSequenceNumber() = 0;
54 virtual WebRtc_UWord16 SequenceNumber() const = 0;
55 virtual WebRtc_UWord16 MaxPayloadLength() const = 0;
henrik.lundin@webrtc.orgf15fbc32011-11-08 08:23:47 +000056 virtual WebRtc_UWord16 MaxDataPayloadLength() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000057 virtual WebRtc_UWord16 PacketOverHead() const = 0;
58 virtual WebRtc_UWord16 TargetSendBitrateKbit() const = 0;
59 virtual WebRtc_UWord16 ActualSendBitrateKbit() const = 0;
60
61 virtual WebRtc_Word32 SendToNetwork(const WebRtc_UWord8* dataBuffer,
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +000062 const WebRtc_UWord16 payloadLength,
63 const WebRtc_UWord16 rtpHeaderLength,
64 const StorageType storage) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000065};
66
67class RTPSender : public Bitrate, public RTPSenderInterface
68{
69public:
pwestin@webrtc.org0644b1d2011-12-01 15:42:31 +000070 RTPSender(const WebRtc_Word32 id, const bool audio, RtpRtcpClock* clock);
niklase@google.com470e71d2011-07-07 08:21:25 +000071 virtual ~RTPSender();
72
73 WebRtc_Word32 Init(const WebRtc_UWord32 remoteSSRC);
74 void ChangeUniqueId(const WebRtc_Word32 id);
75
76 void ProcessBitrate();
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000077 void ProcessSendToNetwork();
niklase@google.com470e71d2011-07-07 08:21:25 +000078
79 WebRtc_UWord16 TargetSendBitrateKbit() const;
80 WebRtc_UWord16 ActualSendBitrateKbit() const;
81
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +000082 WebRtc_UWord32 VideoBitrateSent() const;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +000083 WebRtc_UWord32 FecOverheadRate() const;
84 WebRtc_UWord32 NackOverheadRate() const;
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086 WebRtc_Word32 SetTargetSendBitrate(const WebRtc_UWord32 bits);
87
88 WebRtc_UWord16 MaxDataPayloadLength() const; // with RTP and FEC headers
89
90 // callback
91 WebRtc_Word32 RegisterSendTransport(Transport* outgoingTransport);
92
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000093 WebRtc_Word32 RegisterPayload(
94 const WebRtc_Word8 payloadName[RTP_PAYLOAD_NAME_SIZE],
95 const WebRtc_Word8 payloadType,
96 const WebRtc_UWord32 frequency,
97 const WebRtc_UWord8 channels,
98 const WebRtc_UWord32 rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
100 WebRtc_Word32 DeRegisterSendPayload(const WebRtc_Word8 payloadType);
101
102 WebRtc_Word8 SendPayloadType() const;
103
104 int SendPayloadFrequency() const;
105
106 void SetSendingStatus(const bool enabled);
107
108 void SetSendingMediaStatus(const bool enabled);
109 bool SendingMedia() const;
110
111 // number of sent RTP packets
112 WebRtc_UWord32 Packets() const;
113
114 // number of sent RTP bytes
115 WebRtc_UWord32 Bytes() const;
116
117 WebRtc_Word32 ResetDataCounters();
118
119 WebRtc_UWord32 StartTimestamp() const;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000120 WebRtc_Word32 SetStartTimestamp(const WebRtc_UWord32 timestamp,
121 const bool force = false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
123 WebRtc_UWord32 GenerateNewSSRC();
124 WebRtc_Word32 SetSSRC( const WebRtc_UWord32 ssrc);
125
126 WebRtc_UWord16 SequenceNumber() const;
127 WebRtc_Word32 SetSequenceNumber( WebRtc_UWord16 seq);
128
129 WebRtc_Word32 CSRCs(WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize]) const;
130
131 WebRtc_Word32 SetCSRCStatus(const bool include);
132
133 WebRtc_Word32 SetCSRCs(const WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize],
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000134 const WebRtc_UWord8 arrLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
136 WebRtc_Word32 SetMaxPayloadLength(const WebRtc_UWord16 length,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000137 const WebRtc_UWord16 packetOverHead);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000139 WebRtc_Word32 SendOutgoingData(const FrameType frameType,
140 const WebRtc_Word8 payloadType,
141 const WebRtc_UWord32 timeStamp,
142 const WebRtc_UWord8* payloadData,
143 const WebRtc_UWord32 payloadSize,
144 const RTPFragmentationHeader* fragmentation,
145 VideoCodecInformation* codecInfo = NULL,
146 const RTPVideoTypeHeader* rtpTypeHdr = NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000148 WebRtc_Word32 SendPadData(WebRtc_Word8 payload_type,
149 WebRtc_UWord32 capture_timestamp,
150 WebRtc_Word32 bytes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 /*
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000152 * RTP header extension
153 */
154 WebRtc_Word32 SetTransmissionTimeOffset(
155 const WebRtc_Word32 transmissionTimeOffset);
156
157 WebRtc_Word32 RegisterRtpHeaderExtension(const RTPExtensionType type,
158 const WebRtc_UWord8 id);
159
160 WebRtc_Word32 DeregisterRtpHeaderExtension(const RTPExtensionType type);
161
162 WebRtc_UWord16 RtpHeaderExtensionTotalLength() const;
163
164 WebRtc_UWord16 BuildRTPHeaderExtension(WebRtc_UWord8* dataBuffer) const;
165
166 WebRtc_UWord8 BuildTransmissionTimeOffsetExtension(
167 WebRtc_UWord8* dataBuffer) const;
168
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000169 void UpdateTransmissionTimeOffset(WebRtc_UWord8* rtp_packet,
170 const WebRtc_UWord16 rtp_packet_length,
171 const WebRtcRTPHeader& rtp_header,
172 const WebRtc_UWord32 time_ms) const;
173
174 void SetTransmissionSmoothingStatus(const bool enable);
175
176 bool TransmissionSmoothingStatus() const;
177
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000178 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 * NACK
180 */
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000181 int SelectiveRetransmissions() const;
182 int SetSelectiveRetransmissions(uint8_t settings);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 void OnReceivedNACK(const WebRtc_UWord16 nackSequenceNumbersLength,
184 const WebRtc_UWord16* nackSequenceNumbers,
185 const WebRtc_UWord16 avgRTT);
186
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000187 WebRtc_Word32 SetStorePacketsStatus(const bool enable,
188 const WebRtc_UWord16 numberToStore);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
190 bool StorePackets() const;
191
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000192 WebRtc_Word32 ReSendPacket(WebRtc_UWord16 packet_id,
193 WebRtc_UWord32 min_resend_time = 0);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000194
195 WebRtc_Word32 ReSendToNetwork(const WebRtc_UWord8* packet,
196 const WebRtc_UWord32 size);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
198 bool ProcessNACKBitRate(const WebRtc_UWord32 now);
199
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 /*
201 * Keep alive
202 */
203 WebRtc_Word32 EnableRTPKeepalive( const WebRtc_Word8 unknownPayloadType,
204 const WebRtc_UWord16 deltaTransmitTimeMS);
205
206 WebRtc_Word32 RTPKeepaliveStatus(bool* enable,
207 WebRtc_Word8* unknownPayloadType,
208 WebRtc_UWord16* deltaTransmitTimeMS) const;
209
210 WebRtc_Word32 DisableRTPKeepalive();
211
212 bool RTPKeepalive() const;
213
214 bool TimeToSendRTPKeepalive() const;
215
216 WebRtc_Word32 SendRTPKeepalivePacket();
217
218 /*
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000219 * RTX
220 */
221 void SetRTXStatus(const bool enable,
222 const bool setSSRC,
223 const WebRtc_UWord32 SSRC);
224
225 void RTXStatus(bool* enable, WebRtc_UWord32* SSRC) const;
226
227 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 * Functions wrapping RTPSenderInterface
229 */
230 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
231 const WebRtc_Word8 payloadType,
232 const bool markerBit,
233 const WebRtc_UWord32 captureTimeStamp,
234 const bool timeStampProvided = true,
235 const bool incSequenceNumber = true);
236
237 virtual WebRtc_UWord16 RTPHeaderLength() const ;
238 virtual WebRtc_UWord16 IncrementSequenceNumber();
239 virtual WebRtc_UWord16 MaxPayloadLength() const;
240 virtual WebRtc_UWord16 PacketOverHead() const;
241
242 // current timestamp
243 virtual WebRtc_UWord32 Timestamp() const;
244 virtual WebRtc_UWord32 SSRC() const;
245
246 virtual WebRtc_Word32 SendToNetwork(const WebRtc_UWord8* dataBuffer,
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000247 const WebRtc_UWord16 payloadLength,
248 const WebRtc_UWord16 rtpHeaderLength,
249 const StorageType storage);
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
251 /*
252 * Audio
253 */
254 WebRtc_Word32 RegisterAudioCallback(RtpAudioFeedback* messagesCallback);
255
256 // Send a DTMF tone using RFC 2833 (4733)
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000257 WebRtc_Word32 SendTelephoneEvent(const WebRtc_UWord8 key,
258 const WebRtc_UWord16 time_ms,
259 const WebRtc_UWord8 level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
261 bool SendTelephoneEventActive(WebRtc_Word8& telephoneEvent) const;
262
263 // set audio packet size, used to determine when it's time to send a DTMF packet in silence (CNG)
264 WebRtc_Word32 SetAudioPacketSize(const WebRtc_UWord16 packetSizeSamples);
265
266 // Set status and ID for header-extension-for-audio-level-indication.
267 WebRtc_Word32 SetAudioLevelIndicationStatus(const bool enable,
268 const WebRtc_UWord8 ID);
269
270 // Get status and ID for header-extension-for-audio-level-indication.
271 WebRtc_Word32 AudioLevelIndicationStatus(bool& enable,
272 WebRtc_UWord8& ID) const;
273
274 // Store the audio level in dBov for header-extension-for-audio-level-indication.
275 WebRtc_Word32 SetAudioLevel(const WebRtc_UWord8 level_dBov);
276
277 // Set payload type for Redundant Audio Data RFC 2198
278 WebRtc_Word32 SetRED(const WebRtc_Word8 payloadType);
279
280 // Get payload type for Redundant Audio Data RFC 2198
281 WebRtc_Word32 RED(WebRtc_Word8& payloadType) const;
282
283 /*
284 * Video
285 */
286 VideoCodecInformation* CodecInformationVideo();
287
288 RtpVideoCodecTypes VideoCodecType() const;
289
290 WebRtc_UWord32 MaxConfiguredBitrateVideo() const;
291
292 WebRtc_Word32 SendRTPIntraRequest();
293
294 // FEC
295 WebRtc_Word32 SetGenericFECStatus(const bool enable,
296 const WebRtc_UWord8 payloadTypeRED,
297 const WebRtc_UWord8 payloadTypeFEC);
298
299 WebRtc_Word32 GenericFECStatus(bool& enable,
300 WebRtc_UWord8& payloadTypeRED,
301 WebRtc_UWord8& payloadTypeFEC) const;
302
303 WebRtc_Word32 SetFECCodeRate(const WebRtc_UWord8 keyFrameCodeRate,
marpan@google.com80c5d7a2011-07-15 21:32:40 +0000304 const WebRtc_UWord8 deltaFrameCodeRate);
305
306 WebRtc_Word32 SetFECUepProtection(const bool keyUseUepProtection,
307 const bool deltaUseUepProtection);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
309protected:
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000310 WebRtc_Word32 CheckPayloadType(const WebRtc_Word8 payloadType,
311 RtpVideoCodecTypes& videoType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000312
313private:
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000314 void UpdateNACKBitRate(const WebRtc_UWord32 bytes,
315 const WebRtc_UWord32 now);
316
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000317 WebRtc_Word32 _id;
318 const bool _audioConfigured;
319 RTPSenderAudio* _audio;
320 RTPSenderVideo* _video;
321
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000322 CriticalSectionWrapper* _sendCritsect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000324 CriticalSectionWrapper* _transportCritsect;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000325 Transport* _transport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
327 bool _sendingMedia;
328
329 WebRtc_UWord16 _maxPayloadLength;
330 WebRtc_UWord16 _targetSendBitrate;
331 WebRtc_UWord16 _packetOverHead;
332
333 WebRtc_Word8 _payloadType;
334 MapWrapper _payloadTypeMap;
335
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000336 RtpHeaderExtensionMap _rtpHeaderExtensionMap;
337 WebRtc_Word32 _transmissionTimeOffset;
338
niklase@google.com470e71d2011-07-07 08:21:25 +0000339 bool _keepAliveIsActive;
340 WebRtc_Word8 _keepAlivePayloadType;
341 WebRtc_UWord32 _keepAliveLastSent;
342 WebRtc_UWord16 _keepAliveDeltaTimeSend;
343
niklase@google.com470e71d2011-07-07 08:21:25 +0000344 // NACK
345 WebRtc_UWord32 _nackByteCountTimes[NACK_BYTECOUNT_SIZE];
346 WebRtc_Word32 _nackByteCount[NACK_BYTECOUNT_SIZE];
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000347 Bitrate _nackBitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000349 RTPPacketHistory* _packetHistory;
350 TransmissionBucket _sendBucket;
351 WebRtc_UWord32 _timeLastSendToNetworkUpdate;
352 bool _transmissionSmoothing;
353
niklase@google.com470e71d2011-07-07 08:21:25 +0000354 // statistics
355 WebRtc_UWord32 _packetsSent;
356 WebRtc_UWord32 _payloadBytesSent;
357
358 // RTP variables
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000359 bool _startTimeStampForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000360 WebRtc_UWord32 _startTimeStamp;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000361 SSRCDatabase& _ssrcDB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 WebRtc_UWord32 _remoteSSRC;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000363 bool _sequenceNumberForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000364 WebRtc_UWord16 _sequenceNumber;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000365 WebRtc_UWord16 _sequenceNumberRTX;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000366 bool _ssrcForced;
niklase@google.com470e71d2011-07-07 08:21:25 +0000367 WebRtc_UWord32 _ssrc;
368 WebRtc_UWord32 _timeStamp;
369 WebRtc_UWord8 _CSRCs;
370 WebRtc_UWord32 _CSRC[kRtpCsrcSize];
371 bool _includeCSRCs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000372 bool _RTX;
373 WebRtc_UWord32 _ssrcRTX;
niklase@google.com470e71d2011-07-07 08:21:25 +0000374};
375} // namespace webrtc
376
377#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_