1 /* 2 * Copyright (c) 2012 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 <assert.h> 15 #include <math.h> 16 17 #include <map> 18 19 #include "webrtc/base/thread_annotations.h" 20 #include "webrtc/common_types.h" 21 #include "webrtc/modules/pacing/include/paced_sender.h" 22 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" 23 #include "webrtc/modules/rtp_rtcp/source/bitrate.h" 24 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h" 25 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h" 26 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h" 27 #include "webrtc/modules/rtp_rtcp/source/ssrc_database.h" 28 #include "webrtc/modules/rtp_rtcp/source/video_codec_information.h" 29 30 #define MAX_INIT_RTP_SEQ_NUMBER 32767 // 2^15 -1. 31 32 namespace webrtc { 33 34 class CriticalSectionWrapper; 35 class RTPSenderAudio; 36 class RTPSenderVideo; 37 38 class RTPSenderInterface { 39 public: RTPSenderInterface()40 RTPSenderInterface() {} ~RTPSenderInterface()41 virtual ~RTPSenderInterface() {} 42 43 virtual uint32_t SSRC() const = 0; 44 virtual uint32_t Timestamp() const = 0; 45 46 virtual int32_t BuildRTPheader(uint8_t* data_buffer, 47 const int8_t payload_type, 48 const bool marker_bit, 49 const uint32_t capture_timestamp, 50 int64_t capture_time_ms, 51 const bool timestamp_provided = true, 52 const bool inc_sequence_number = true) = 0; 53 54 virtual uint16_t RTPHeaderLength() const = 0; 55 virtual uint16_t IncrementSequenceNumber() = 0; 56 virtual uint16_t SequenceNumber() const = 0; 57 virtual uint16_t MaxPayloadLength() const = 0; 58 virtual uint16_t MaxDataPayloadLength() const = 0; 59 virtual uint16_t PacketOverHead() const = 0; 60 virtual uint16_t ActualSendBitrateKbit() const = 0; 61 62 virtual int32_t SendToNetwork( 63 uint8_t *data_buffer, int payload_length, int rtp_header_length, 64 int64_t capture_time_ms, StorageType storage, 65 PacedSender::Priority priority) = 0; 66 }; 67 68 class RTPSender : public RTPSenderInterface, public Bitrate::Observer { 69 public: 70 RTPSender(const int32_t id, const bool audio, Clock *clock, 71 Transport *transport, RtpAudioFeedback *audio_feedback, 72 PacedSender *paced_sender, 73 BitrateStatisticsObserver* bitrate_callback, 74 FrameCountObserver* frame_count_observer, 75 SendSideDelayObserver* send_side_delay_observer); 76 virtual ~RTPSender(); 77 78 void ProcessBitrate(); 79 80 virtual uint16_t ActualSendBitrateKbit() const OVERRIDE; 81 82 uint32_t VideoBitrateSent() const; 83 uint32_t FecOverheadRate() const; 84 uint32_t NackOverheadRate() const; 85 86 // Returns true if the statistics have been calculated, and false if no frame 87 // was sent within the statistics window. 88 bool GetSendSideDelay(int* avg_send_delay_ms, int* max_send_delay_ms) const; 89 90 void SetTargetBitrate(uint32_t bitrate); 91 uint32_t GetTargetBitrate(); 92 93 virtual uint16_t MaxDataPayloadLength() const 94 OVERRIDE; // with RTP and FEC headers. 95 96 int32_t RegisterPayload( 97 const char payload_name[RTP_PAYLOAD_NAME_SIZE], 98 const int8_t payload_type, const uint32_t frequency, 99 const uint8_t channels, const uint32_t rate); 100 101 int32_t DeRegisterSendPayload(const int8_t payload_type); 102 103 void SetSendPayloadType(int8_t payload_type); 104 105 int8_t SendPayloadType() const; 106 107 int SendPayloadFrequency() const; 108 109 void SetSendingStatus(bool enabled); 110 111 void SetSendingMediaStatus(const bool enabled); 112 bool SendingMedia() const; 113 114 void GetDataCounters(StreamDataCounters* rtp_stats, 115 StreamDataCounters* rtx_stats) const; 116 117 void ResetDataCounters(); 118 119 uint32_t StartTimestamp() const; 120 void SetStartTimestamp(uint32_t timestamp, bool force); 121 122 uint32_t GenerateNewSSRC(); 123 void SetSSRC(const uint32_t ssrc); 124 125 virtual uint16_t SequenceNumber() const OVERRIDE; 126 void SetSequenceNumber(uint16_t seq); 127 128 int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const; 129 130 void SetCSRCStatus(const bool include); 131 132 void SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize], 133 const uint8_t arr_length); 134 135 int32_t SetMaxPayloadLength(const uint16_t length, 136 const uint16_t packet_over_head); 137 138 int32_t SendOutgoingData(const FrameType frame_type, 139 const int8_t payload_type, 140 const uint32_t timestamp, 141 int64_t capture_time_ms, 142 const uint8_t* payload_data, 143 const uint32_t payload_size, 144 const RTPFragmentationHeader* fragmentation, 145 VideoCodecInformation* codec_info = NULL, 146 const RTPVideoTypeHeader* rtp_type_hdr = NULL); 147 148 // RTP header extension 149 int32_t SetTransmissionTimeOffset( 150 const int32_t transmission_time_offset); 151 int32_t SetAbsoluteSendTime( 152 const uint32_t absolute_send_time); 153 154 int32_t RegisterRtpHeaderExtension(const RTPExtensionType type, 155 const uint8_t id); 156 157 int32_t DeregisterRtpHeaderExtension(const RTPExtensionType type); 158 159 uint16_t RtpHeaderExtensionTotalLength() const; 160 161 uint16_t BuildRTPHeaderExtension(uint8_t* data_buffer) const; 162 163 uint8_t BuildTransmissionTimeOffsetExtension(uint8_t *data_buffer) const; 164 uint8_t BuildAudioLevelExtension(uint8_t* data_buffer) const; 165 uint8_t BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const; 166 167 bool UpdateAudioLevel(uint8_t *rtp_packet, 168 const uint16_t rtp_packet_length, 169 const RTPHeader &rtp_header, 170 const bool is_voiced, 171 const uint8_t dBov) const; 172 173 bool TimeToSendPacket(uint16_t sequence_number, int64_t capture_time_ms, 174 bool retransmission); 175 int TimeToSendPadding(int bytes); 176 177 // NACK. 178 int SelectiveRetransmissions() const; 179 int SetSelectiveRetransmissions(uint8_t settings); 180 void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers, 181 const uint16_t avg_rtt); 182 183 void SetStorePacketsStatus(const bool enable, 184 const uint16_t number_to_store); 185 186 bool StorePackets() const; 187 188 int32_t ReSendPacket(uint16_t packet_id, uint32_t min_resend_time = 0); 189 190 bool ProcessNACKBitRate(const uint32_t now); 191 192 // RTX. 193 void SetRTXStatus(int mode); 194 195 void RTXStatus(int* mode, uint32_t* ssrc, int* payload_type) const; 196 197 uint32_t RtxSsrc() const; 198 void SetRtxSsrc(uint32_t ssrc); 199 200 void SetRtxPayloadType(int payloadType); 201 202 // Functions wrapping RTPSenderInterface. 203 virtual int32_t BuildRTPheader( 204 uint8_t* data_buffer, 205 const int8_t payload_type, 206 const bool marker_bit, 207 const uint32_t capture_timestamp, 208 int64_t capture_time_ms, 209 const bool timestamp_provided = true, 210 const bool inc_sequence_number = true) OVERRIDE; 211 212 virtual uint16_t RTPHeaderLength() const OVERRIDE; 213 virtual uint16_t IncrementSequenceNumber() OVERRIDE; 214 virtual uint16_t MaxPayloadLength() const OVERRIDE; 215 virtual uint16_t PacketOverHead() const OVERRIDE; 216 217 // Current timestamp. 218 virtual uint32_t Timestamp() const OVERRIDE; 219 virtual uint32_t SSRC() const OVERRIDE; 220 221 virtual int32_t SendToNetwork( 222 uint8_t *data_buffer, int payload_length, int rtp_header_length, 223 int64_t capture_time_ms, StorageType storage, 224 PacedSender::Priority priority) OVERRIDE; 225 226 // Audio. 227 228 // Send a DTMF tone using RFC 2833 (4733). 229 int32_t SendTelephoneEvent(const uint8_t key, 230 const uint16_t time_ms, 231 const uint8_t level); 232 233 bool SendTelephoneEventActive(int8_t *telephone_event) const; 234 235 // Set audio packet size, used to determine when it's time to send a DTMF 236 // packet in silence (CNG). 237 int32_t SetAudioPacketSize(const uint16_t packet_size_samples); 238 239 // Store the audio level in d_bov for 240 // header-extension-for-audio-level-indication. 241 int32_t SetAudioLevel(const uint8_t level_d_bov); 242 243 // Set payload type for Redundant Audio Data RFC 2198. 244 int32_t SetRED(const int8_t payload_type); 245 246 // Get payload type for Redundant Audio Data RFC 2198. 247 int32_t RED(int8_t *payload_type) const; 248 249 // Video. 250 VideoCodecInformation *CodecInformationVideo(); 251 252 RtpVideoCodecTypes VideoCodecType() const; 253 254 uint32_t MaxConfiguredBitrateVideo() const; 255 256 int32_t SendRTPIntraRequest(); 257 258 // FEC. 259 int32_t SetGenericFECStatus(const bool enable, 260 const uint8_t payload_type_red, 261 const uint8_t payload_type_fec); 262 263 int32_t GenericFECStatus(bool *enable, uint8_t *payload_type_red, 264 uint8_t *payload_type_fec) const; 265 266 int32_t SetFecParameters(const FecProtectionParams *delta_params, 267 const FecProtectionParams *key_params); 268 269 int SendPadData(uint32_t timestamp, 270 int64_t capture_time_ms, 271 int32_t bytes); 272 273 // Called on update of RTP statistics. 274 void RegisterRtpStatisticsCallback(StreamDataCountersCallback* callback); 275 StreamDataCountersCallback* GetRtpStatisticsCallback() const; 276 277 uint32_t BitrateSent() const; 278 279 virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE; 280 281 void SetRtpState(const RtpState& rtp_state); 282 RtpState GetRtpState() const; 283 void SetRtxRtpState(const RtpState& rtp_state); 284 RtpState GetRtxRtpState() const; 285 286 protected: 287 int32_t CheckPayloadType(const int8_t payload_type, 288 RtpVideoCodecTypes *video_type); 289 290 private: 291 // Maps capture time in milliseconds to send-side delay in milliseconds. 292 // Send-side delay is the difference between transmission time and capture 293 // time. 294 typedef std::map<int64_t, int> SendDelayMap; 295 296 int CreateRTPHeader(uint8_t* header, int8_t payload_type, 297 uint32_t ssrc, bool marker_bit, 298 uint32_t timestamp, uint16_t sequence_number, 299 const uint32_t* csrcs, uint8_t csrcs_length) const; 300 301 void UpdateNACKBitRate(const uint32_t bytes, const uint32_t now); 302 303 bool PrepareAndSendPacket(uint8_t* buffer, 304 uint16_t length, 305 int64_t capture_time_ms, 306 bool send_over_rtx, 307 bool is_retransmit); 308 309 // Return the number of bytes sent. 310 int TrySendRedundantPayloads(int bytes); 311 int TrySendPadData(int bytes); 312 313 int BuildPaddingPacket(uint8_t* packet, int header_length, int32_t bytes); 314 315 void BuildRtxPacket(uint8_t* buffer, uint16_t* length, 316 uint8_t* buffer_rtx); 317 318 bool SendPacketToNetwork(const uint8_t *packet, uint32_t size); 319 320 void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms); 321 322 void UpdateTransmissionTimeOffset(uint8_t *rtp_packet, 323 const uint16_t rtp_packet_length, 324 const RTPHeader &rtp_header, 325 const int64_t time_diff_ms) const; 326 void UpdateAbsoluteSendTime(uint8_t *rtp_packet, 327 const uint16_t rtp_packet_length, 328 const RTPHeader &rtp_header, 329 const int64_t now_ms) const; 330 331 void UpdateRtpStats(const uint8_t* buffer, 332 uint32_t size, 333 const RTPHeader& header, 334 bool is_rtx, 335 bool is_retransmit); 336 bool IsFecPacket(const uint8_t* buffer, const RTPHeader& header) const; 337 338 Clock* clock_; 339 Bitrate bitrate_sent_; 340 341 int32_t id_; 342 const bool audio_configured_; 343 RTPSenderAudio *audio_; 344 RTPSenderVideo *video_; 345 346 PacedSender *paced_sender_; 347 CriticalSectionWrapper *send_critsect_; 348 349 Transport *transport_; 350 bool sending_media_ GUARDED_BY(send_critsect_); 351 352 uint16_t max_payload_length_; 353 uint16_t packet_over_head_; 354 355 int8_t payload_type_ GUARDED_BY(send_critsect_); 356 std::map<int8_t, RtpUtility::Payload*> payload_type_map_; 357 358 RtpHeaderExtensionMap rtp_header_extension_map_; 359 int32_t transmission_time_offset_; 360 uint32_t absolute_send_time_; 361 362 // NACK 363 uint32_t nack_byte_count_times_[NACK_BYTECOUNT_SIZE]; 364 int32_t nack_byte_count_[NACK_BYTECOUNT_SIZE]; 365 Bitrate nack_bitrate_; 366 367 RTPPacketHistory packet_history_; 368 369 // Statistics 370 scoped_ptr<CriticalSectionWrapper> statistics_crit_; 371 SendDelayMap send_delays_ GUARDED_BY(statistics_crit_); 372 std::map<FrameType, uint32_t> frame_counts_ GUARDED_BY(statistics_crit_); 373 StreamDataCounters rtp_stats_ GUARDED_BY(statistics_crit_); 374 StreamDataCounters rtx_rtp_stats_ GUARDED_BY(statistics_crit_); 375 StreamDataCountersCallback* rtp_stats_callback_ GUARDED_BY(statistics_crit_); 376 BitrateStatisticsObserver* const bitrate_callback_; 377 FrameCountObserver* const frame_count_observer_; 378 SendSideDelayObserver* const send_side_delay_observer_; 379 380 // RTP variables 381 bool start_timestamp_forced_ GUARDED_BY(send_critsect_); 382 uint32_t start_timestamp_ GUARDED_BY(send_critsect_); 383 SSRCDatabase& ssrc_db_ GUARDED_BY(send_critsect_); 384 uint32_t remote_ssrc_ GUARDED_BY(send_critsect_); 385 bool sequence_number_forced_ GUARDED_BY(send_critsect_); 386 uint16_t sequence_number_ GUARDED_BY(send_critsect_); 387 uint16_t sequence_number_rtx_ GUARDED_BY(send_critsect_); 388 bool ssrc_forced_ GUARDED_BY(send_critsect_); 389 uint32_t ssrc_ GUARDED_BY(send_critsect_); 390 uint32_t timestamp_ GUARDED_BY(send_critsect_); 391 int64_t capture_time_ms_ GUARDED_BY(send_critsect_); 392 int64_t last_timestamp_time_ms_ GUARDED_BY(send_critsect_); 393 bool media_has_been_sent_ GUARDED_BY(send_critsect_); 394 bool last_packet_marker_bit_ GUARDED_BY(send_critsect_); 395 uint8_t num_csrcs_ GUARDED_BY(send_critsect_); 396 uint32_t csrcs_[kRtpCsrcSize] GUARDED_BY(send_critsect_); 397 bool include_csrcs_ GUARDED_BY(send_critsect_); 398 int rtx_ GUARDED_BY(send_critsect_); 399 uint32_t ssrc_rtx_ GUARDED_BY(send_critsect_); 400 int payload_type_rtx_ GUARDED_BY(send_critsect_); 401 402 // Note: Don't access this variable directly, always go through 403 // SetTargetBitrateKbps or GetTargetBitrateKbps. Also remember 404 // that by the time the function returns there is no guarantee 405 // that the target bitrate is still valid. 406 scoped_ptr<CriticalSectionWrapper> target_bitrate_critsect_; 407 uint32_t target_bitrate_ GUARDED_BY(target_bitrate_critsect_); 408 }; 409 410 } // namespace webrtc 411 412 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ 413