blob: 0f27883affc344dd1e809b0c406bc3f9784c6267 [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
14#include "rtp_rtcp_config.h" // misc. defines (e.g. MAX_PACKET_LENGTH)
15#include "common_types.h" // Encryption
16#include "ssrc_database.h"
17#include "list_wrapper.h"
18#include "map_wrapper.h"
19#include "Bitrate.h"
20#include "video_codec_information.h"
21
22#include <cassert>
23#include <cmath>
24
25#define MAX_INIT_RTP_SEQ_NUMBER 32767 // 2^15 -1
26
27namespace webrtc {
28class CriticalSectionWrapper;
29class RTPSenderAudio;
30class RTPSenderVideo;
31
32class RTPSenderInterface
33{
34public:
35 RTPSenderInterface() {}
36 virtual ~RTPSenderInterface() {}
37
38 virtual WebRtc_UWord32 SSRC() const = 0;
39 virtual WebRtc_UWord32 Timestamp() const = 0;
40
41 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
42 const WebRtc_Word8 payloadType,
43 const bool markerBit,
44 const WebRtc_UWord32 captureTimeStamp,
45 const bool timeStampProvided = true,
46 const bool incSequenceNumber = true) = 0;
47
48 virtual WebRtc_UWord16 RTPHeaderLength() const = 0;
49 virtual WebRtc_UWord16 IncrementSequenceNumber() = 0;
50 virtual WebRtc_UWord16 SequenceNumber() const = 0;
51 virtual WebRtc_UWord16 MaxPayloadLength() const = 0;
henrik.lundin@webrtc.orgf15fbc32011-11-08 08:23:47 +000052 virtual WebRtc_UWord16 MaxDataPayloadLength() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000053 virtual WebRtc_UWord16 PacketOverHead() const = 0;
54 virtual WebRtc_UWord16 TargetSendBitrateKbit() const = 0;
55 virtual WebRtc_UWord16 ActualSendBitrateKbit() const = 0;
56
57 virtual WebRtc_Word32 SendToNetwork(const WebRtc_UWord8* dataBuffer,
58 const WebRtc_UWord16 payloadLength,
59 const WebRtc_UWord16 rtpHeaderLength,
60 const bool dontStore = false) = 0;
61};
62
63class RTPSender : public Bitrate, public RTPSenderInterface
64{
65public:
66 RTPSender(const WebRtc_Word32 id, const bool audio);
67 virtual ~RTPSender();
68
69 WebRtc_Word32 Init(const WebRtc_UWord32 remoteSSRC);
70 void ChangeUniqueId(const WebRtc_Word32 id);
71
72 void ProcessBitrate();
73
74 WebRtc_UWord16 TargetSendBitrateKbit() const;
75 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
niklase@google.com470e71d2011-07-07 08:21:25 +000081 WebRtc_Word32 SetTargetSendBitrate(const WebRtc_UWord32 bits);
82
83 WebRtc_UWord16 MaxDataPayloadLength() const; // with RTP and FEC headers
84
85 // callback
86 WebRtc_Word32 RegisterSendTransport(Transport* outgoingTransport);
87
88 WebRtc_Word32 RegisterPayload(const WebRtc_Word8 payloadName[RTP_PAYLOAD_NAME_SIZE],
89 const WebRtc_Word8 payloadType,
90 const WebRtc_UWord32 frequency,
91 const WebRtc_UWord8 channels,
92 const WebRtc_UWord32 rate);
93
94 WebRtc_Word32 DeRegisterSendPayload(const WebRtc_Word8 payloadType);
95
96 WebRtc_Word8 SendPayloadType() const;
97
98 int SendPayloadFrequency() const;
99
100 void SetSendingStatus(const bool enabled);
101
102 void SetSendingMediaStatus(const bool enabled);
103 bool SendingMedia() const;
104
105 // number of sent RTP packets
106 WebRtc_UWord32 Packets() const;
107
108 // number of sent RTP bytes
109 WebRtc_UWord32 Bytes() const;
110
111 WebRtc_Word32 ResetDataCounters();
112
113 WebRtc_UWord32 StartTimestamp() const;
114 WebRtc_Word32 SetStartTimestamp( const WebRtc_UWord32 timestamp, const bool force = false);
115
116 WebRtc_UWord32 GenerateNewSSRC();
117 WebRtc_Word32 SetSSRC( const WebRtc_UWord32 ssrc);
118
119 WebRtc_UWord16 SequenceNumber() const;
120 WebRtc_Word32 SetSequenceNumber( WebRtc_UWord16 seq);
121
122 WebRtc_Word32 CSRCs(WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize]) const;
123
124 WebRtc_Word32 SetCSRCStatus(const bool include);
125
126 WebRtc_Word32 SetCSRCs(const WebRtc_UWord32 arrOfCSRC[kRtpCsrcSize],
127 const WebRtc_UWord8 arrLength);
128
129 WebRtc_Word32 SetMaxPayloadLength(const WebRtc_UWord16 length,
130 const WebRtc_UWord16 packetOverHead);
131
132 WebRtc_Word32
133 SendOutgoingData(const FrameType frameType,
134 const WebRtc_Word8 payloadType,
135 const WebRtc_UWord32 timeStamp,
136 const WebRtc_UWord8* payloadData,
137 const WebRtc_UWord32 payloadSize,
138 const RTPFragmentationHeader* fragmentation,
139 VideoCodecInformation* codecInfo = NULL,
140 const RTPVideoTypeHeader* rtpTypeHdr = NULL);
141
142 /*
143 * NACK
144 */
145 void OnReceivedNACK(const WebRtc_UWord16 nackSequenceNumbersLength,
146 const WebRtc_UWord16* nackSequenceNumbers,
147 const WebRtc_UWord16 avgRTT);
148
149 WebRtc_Word32 SetStorePacketsStatus(const bool enable, const WebRtc_UWord16 numberToStore);
150
151 bool StorePackets() const;
152
153 WebRtc_Word32 ReSendToNetwork(WebRtc_UWord16 packetID,
154 WebRtc_UWord32 minResendTime=0);
155
156 bool ProcessNACKBitRate(const WebRtc_UWord32 now);
157
158 void UpdateNACKBitRate( const WebRtc_UWord32 bytes,
159 const WebRtc_UWord32 now);
160
161 /*
162 * Keep alive
163 */
164 WebRtc_Word32 EnableRTPKeepalive( const WebRtc_Word8 unknownPayloadType,
165 const WebRtc_UWord16 deltaTransmitTimeMS);
166
167 WebRtc_Word32 RTPKeepaliveStatus(bool* enable,
168 WebRtc_Word8* unknownPayloadType,
169 WebRtc_UWord16* deltaTransmitTimeMS) const;
170
171 WebRtc_Word32 DisableRTPKeepalive();
172
173 bool RTPKeepalive() const;
174
175 bool TimeToSendRTPKeepalive() const;
176
177 WebRtc_Word32 SendRTPKeepalivePacket();
178
179 /*
180 * Functions wrapping RTPSenderInterface
181 */
182 virtual WebRtc_Word32 BuildRTPheader(WebRtc_UWord8* dataBuffer,
183 const WebRtc_Word8 payloadType,
184 const bool markerBit,
185 const WebRtc_UWord32 captureTimeStamp,
186 const bool timeStampProvided = true,
187 const bool incSequenceNumber = true);
188
189 virtual WebRtc_UWord16 RTPHeaderLength() const ;
190 virtual WebRtc_UWord16 IncrementSequenceNumber();
191 virtual WebRtc_UWord16 MaxPayloadLength() const;
192 virtual WebRtc_UWord16 PacketOverHead() const;
193
194 // current timestamp
195 virtual WebRtc_UWord32 Timestamp() const;
196 virtual WebRtc_UWord32 SSRC() const;
197
198 virtual WebRtc_Word32 SendToNetwork(const WebRtc_UWord8* dataBuffer,
199 const WebRtc_UWord16 payloadLength,
200 const WebRtc_UWord16 rtpHeaderLength,
201 const bool dontStore = false);
202
203 /*
204 * Audio
205 */
206 WebRtc_Word32 RegisterAudioCallback(RtpAudioFeedback* messagesCallback);
207
208 // Send a DTMF tone using RFC 2833 (4733)
209 WebRtc_Word32 SendTelephoneEvent(const WebRtc_UWord8 key,
210 const WebRtc_UWord16 time_ms,
211 const WebRtc_UWord8 level);
212
213 bool SendTelephoneEventActive(WebRtc_Word8& telephoneEvent) const;
214
215 // set audio packet size, used to determine when it's time to send a DTMF packet in silence (CNG)
216 WebRtc_Word32 SetAudioPacketSize(const WebRtc_UWord16 packetSizeSamples);
217
218 // Set status and ID for header-extension-for-audio-level-indication.
219 WebRtc_Word32 SetAudioLevelIndicationStatus(const bool enable,
220 const WebRtc_UWord8 ID);
221
222 // Get status and ID for header-extension-for-audio-level-indication.
223 WebRtc_Word32 AudioLevelIndicationStatus(bool& enable,
224 WebRtc_UWord8& ID) const;
225
226 // Store the audio level in dBov for header-extension-for-audio-level-indication.
227 WebRtc_Word32 SetAudioLevel(const WebRtc_UWord8 level_dBov);
228
229 // Set payload type for Redundant Audio Data RFC 2198
230 WebRtc_Word32 SetRED(const WebRtc_Word8 payloadType);
231
232 // Get payload type for Redundant Audio Data RFC 2198
233 WebRtc_Word32 RED(WebRtc_Word8& payloadType) const;
234
235 /*
236 * Video
237 */
238 VideoCodecInformation* CodecInformationVideo();
239
240 RtpVideoCodecTypes VideoCodecType() const;
241
242 WebRtc_UWord32 MaxConfiguredBitrateVideo() const;
243
244 WebRtc_Word32 SendRTPIntraRequest();
245
246 // FEC
247 WebRtc_Word32 SetGenericFECStatus(const bool enable,
248 const WebRtc_UWord8 payloadTypeRED,
249 const WebRtc_UWord8 payloadTypeFEC);
250
251 WebRtc_Word32 GenericFECStatus(bool& enable,
252 WebRtc_UWord8& payloadTypeRED,
253 WebRtc_UWord8& payloadTypeFEC) const;
254
255 WebRtc_Word32 SetFECCodeRate(const WebRtc_UWord8 keyFrameCodeRate,
marpan@google.com80c5d7a2011-07-15 21:32:40 +0000256 const WebRtc_UWord8 deltaFrameCodeRate);
257
258 WebRtc_Word32 SetFECUepProtection(const bool keyUseUepProtection,
259 const bool deltaUseUepProtection);
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
261protected:
262 WebRtc_Word32 CheckPayloadType(const WebRtc_Word8 payloadType, RtpVideoCodecTypes& videoType);
263
264private:
265 WebRtc_Word32 _id;
266 const bool _audioConfigured;
267 RTPSenderAudio* _audio;
268 RTPSenderVideo* _video;
269
270 CriticalSectionWrapper& _sendCritsect;
271
272 CriticalSectionWrapper& _transportCritsect;
273 Transport* _transport;
274
275 bool _sendingMedia;
276
277 WebRtc_UWord16 _maxPayloadLength;
278 WebRtc_UWord16 _targetSendBitrate;
279 WebRtc_UWord16 _packetOverHead;
280
281 WebRtc_Word8 _payloadType;
282 MapWrapper _payloadTypeMap;
283
284 bool _keepAliveIsActive;
285 WebRtc_Word8 _keepAlivePayloadType;
286 WebRtc_UWord32 _keepAliveLastSent;
287 WebRtc_UWord16 _keepAliveDeltaTimeSend;
288
289 bool _storeSentPackets;
290 WebRtc_UWord16 _storeSentPacketsNumber;
291 CriticalSectionWrapper& _prevSentPacketsCritsect;
292 WebRtc_Word32 _prevSentPacketsIndex;
293 WebRtc_Word8** _ptrPrevSentPackets;
294 WebRtc_UWord16* _prevSentPacketsSeqNum;
295 WebRtc_UWord16* _prevSentPacketsLength;
296 WebRtc_UWord32* _prevSentPacketsResendTime;
297
298 // NACK
299 WebRtc_UWord32 _nackByteCountTimes[NACK_BYTECOUNT_SIZE];
300 WebRtc_Word32 _nackByteCount[NACK_BYTECOUNT_SIZE];
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000301 Bitrate _nackBitrate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
303 // statistics
304 WebRtc_UWord32 _packetsSent;
305 WebRtc_UWord32 _payloadBytesSent;
306
307 // RTP variables
308 bool _startTimeStampForced;
309 WebRtc_UWord32 _startTimeStamp;
310 SSRCDatabase& _ssrcDB;
311 WebRtc_UWord32 _remoteSSRC;
312 bool _sequenceNumberForced;
313 WebRtc_UWord16 _sequenceNumber;
314 bool _ssrcForced;
315 WebRtc_UWord32 _ssrc;
316 WebRtc_UWord32 _timeStamp;
317 WebRtc_UWord8 _CSRCs;
318 WebRtc_UWord32 _CSRC[kRtpCsrcSize];
319 bool _includeCSRCs;
320};
321} // namespace webrtc
322
323#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_