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