• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12 
13 #include <stdlib.h>  // srand
14 
15 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
16 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18 #include "webrtc/system_wrappers/interface/logging.h"
19 #include "webrtc/system_wrappers/interface/tick_util.h"
20 #include "webrtc/system_wrappers/interface/trace_event.h"
21 
22 namespace webrtc {
23 
24 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25 const int kMaxPaddingLength = 224;
26 const int kSendSideDelayWindowMs = 1000;
27 
28 namespace {
29 
FrameTypeToString(const FrameType frame_type)30 const char* FrameTypeToString(const FrameType frame_type) {
31   switch (frame_type) {
32     case kFrameEmpty: return "empty";
33     case kAudioFrameSpeech: return "audio_speech";
34     case kAudioFrameCN: return "audio_cn";
35     case kVideoFrameKey: return "video_key";
36     case kVideoFrameDelta: return "video_delta";
37   }
38   return "";
39 }
40 
41 }  // namespace
42 
RTPSender(const int32_t id,const bool audio,Clock * clock,Transport * transport,RtpAudioFeedback * audio_feedback,PacedSender * paced_sender,BitrateStatisticsObserver * bitrate_callback,FrameCountObserver * frame_count_observer,SendSideDelayObserver * send_side_delay_observer)43 RTPSender::RTPSender(const int32_t id,
44                      const bool audio,
45                      Clock* clock,
46                      Transport* transport,
47                      RtpAudioFeedback* audio_feedback,
48                      PacedSender* paced_sender,
49                      BitrateStatisticsObserver* bitrate_callback,
50                      FrameCountObserver* frame_count_observer,
51                      SendSideDelayObserver* send_side_delay_observer)
52     : clock_(clock),
53       bitrate_sent_(clock, this),
54       id_(id),
55       audio_configured_(audio),
56       audio_(NULL),
57       video_(NULL),
58       paced_sender_(paced_sender),
59       send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
60       transport_(transport),
61       sending_media_(true),                      // Default to sending media.
62       max_payload_length_(IP_PACKET_SIZE - 28),  // Default is IP-v4/UDP.
63       packet_over_head_(28),
64       payload_type_(-1),
65       payload_type_map_(),
66       rtp_header_extension_map_(),
67       transmission_time_offset_(0),
68       absolute_send_time_(0),
69       // NACK.
70       nack_byte_count_times_(),
71       nack_byte_count_(),
72       nack_bitrate_(clock, NULL),
73       packet_history_(clock),
74       // Statistics
75       statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
76       rtp_stats_callback_(NULL),
77       bitrate_callback_(bitrate_callback),
78       frame_count_observer_(frame_count_observer),
79       send_side_delay_observer_(send_side_delay_observer),
80       // RTP variables
81       start_timestamp_forced_(false),
82       start_timestamp_(0),
83       ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
84       remote_ssrc_(0),
85       sequence_number_forced_(false),
86       ssrc_forced_(false),
87       timestamp_(0),
88       capture_time_ms_(0),
89       last_timestamp_time_ms_(0),
90       media_has_been_sent_(false),
91       last_packet_marker_bit_(false),
92       num_csrcs_(0),
93       csrcs_(),
94       include_csrcs_(true),
95       rtx_(kRtxOff),
96       payload_type_rtx_(-1),
97       target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
98       target_bitrate_(0) {
99   memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
100   memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
101   memset(csrcs_, 0, sizeof(csrcs_));
102   // We need to seed the random generator.
103   srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
104   ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
105   ssrc_rtx_ = ssrc_db_.CreateSSRC();  // Can't be 0.
106   // Random start, 16 bits. Can't be 0.
107   sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
108   sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
109 
110   if (audio) {
111     audio_ = new RTPSenderAudio(id, clock_, this);
112     audio_->RegisterAudioCallback(audio_feedback);
113   } else {
114     video_ = new RTPSenderVideo(clock_, this);
115   }
116 }
117 
~RTPSender()118 RTPSender::~RTPSender() {
119   if (remote_ssrc_ != 0) {
120     ssrc_db_.ReturnSSRC(remote_ssrc_);
121   }
122   ssrc_db_.ReturnSSRC(ssrc_);
123 
124   SSRCDatabase::ReturnSSRCDatabase();
125   delete send_critsect_;
126   while (!payload_type_map_.empty()) {
127     std::map<int8_t, RtpUtility::Payload*>::iterator it =
128         payload_type_map_.begin();
129     delete it->second;
130     payload_type_map_.erase(it);
131   }
132   delete audio_;
133   delete video_;
134 }
135 
SetTargetBitrate(uint32_t bitrate)136 void RTPSender::SetTargetBitrate(uint32_t bitrate) {
137   CriticalSectionScoped cs(target_bitrate_critsect_.get());
138   target_bitrate_ = bitrate;
139 }
140 
GetTargetBitrate()141 uint32_t RTPSender::GetTargetBitrate() {
142   CriticalSectionScoped cs(target_bitrate_critsect_.get());
143   return target_bitrate_;
144 }
145 
ActualSendBitrateKbit() const146 uint16_t RTPSender::ActualSendBitrateKbit() const {
147   return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
148 }
149 
VideoBitrateSent() const150 uint32_t RTPSender::VideoBitrateSent() const {
151   if (video_) {
152     return video_->VideoBitrateSent();
153   }
154   return 0;
155 }
156 
FecOverheadRate() const157 uint32_t RTPSender::FecOverheadRate() const {
158   if (video_) {
159     return video_->FecOverheadRate();
160   }
161   return 0;
162 }
163 
NackOverheadRate() const164 uint32_t RTPSender::NackOverheadRate() const {
165   return nack_bitrate_.BitrateLast();
166 }
167 
GetSendSideDelay(int * avg_send_delay_ms,int * max_send_delay_ms) const168 bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
169                                  int* max_send_delay_ms) const {
170   CriticalSectionScoped lock(statistics_crit_.get());
171   SendDelayMap::const_iterator it = send_delays_.upper_bound(
172       clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
173   if (it == send_delays_.end())
174     return false;
175   int num_delays = 0;
176   for (; it != send_delays_.end(); ++it) {
177     *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
178     *avg_send_delay_ms += it->second;
179     ++num_delays;
180   }
181   *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
182   return true;
183 }
184 
SetTransmissionTimeOffset(const int32_t transmission_time_offset)185 int32_t RTPSender::SetTransmissionTimeOffset(
186     const int32_t transmission_time_offset) {
187   if (transmission_time_offset > (0x800000 - 1) ||
188       transmission_time_offset < -(0x800000 - 1)) {  // Word24.
189     return -1;
190   }
191   CriticalSectionScoped cs(send_critsect_);
192   transmission_time_offset_ = transmission_time_offset;
193   return 0;
194 }
195 
SetAbsoluteSendTime(const uint32_t absolute_send_time)196 int32_t RTPSender::SetAbsoluteSendTime(
197     const uint32_t absolute_send_time) {
198   if (absolute_send_time > 0xffffff) {  // UWord24.
199     return -1;
200   }
201   CriticalSectionScoped cs(send_critsect_);
202   absolute_send_time_ = absolute_send_time;
203   return 0;
204 }
205 
RegisterRtpHeaderExtension(const RTPExtensionType type,const uint8_t id)206 int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
207                                               const uint8_t id) {
208   CriticalSectionScoped cs(send_critsect_);
209   return rtp_header_extension_map_.Register(type, id);
210 }
211 
DeregisterRtpHeaderExtension(const RTPExtensionType type)212 int32_t RTPSender::DeregisterRtpHeaderExtension(
213     const RTPExtensionType type) {
214   CriticalSectionScoped cs(send_critsect_);
215   return rtp_header_extension_map_.Deregister(type);
216 }
217 
RtpHeaderExtensionTotalLength() const218 uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
219   CriticalSectionScoped cs(send_critsect_);
220   return rtp_header_extension_map_.GetTotalLengthInBytes();
221 }
222 
RegisterPayload(const char payload_name[RTP_PAYLOAD_NAME_SIZE],const int8_t payload_number,const uint32_t frequency,const uint8_t channels,const uint32_t rate)223 int32_t RTPSender::RegisterPayload(
224     const char payload_name[RTP_PAYLOAD_NAME_SIZE],
225     const int8_t payload_number, const uint32_t frequency,
226     const uint8_t channels, const uint32_t rate) {
227   assert(payload_name);
228   CriticalSectionScoped cs(send_critsect_);
229 
230   std::map<int8_t, RtpUtility::Payload*>::iterator it =
231       payload_type_map_.find(payload_number);
232 
233   if (payload_type_map_.end() != it) {
234     // We already use this payload type.
235     RtpUtility::Payload* payload = it->second;
236     assert(payload);
237 
238     // Check if it's the same as we already have.
239     if (RtpUtility::StringCompare(
240             payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
241       if (audio_configured_ && payload->audio &&
242           payload->typeSpecific.Audio.frequency == frequency &&
243           (payload->typeSpecific.Audio.rate == rate ||
244            payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
245         payload->typeSpecific.Audio.rate = rate;
246         // Ensure that we update the rate if new or old is zero.
247         return 0;
248       }
249       if (!audio_configured_ && !payload->audio) {
250         return 0;
251       }
252     }
253     return -1;
254   }
255   int32_t ret_val = -1;
256   RtpUtility::Payload* payload = NULL;
257   if (audio_configured_) {
258     ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
259                                            frequency, channels, rate, payload);
260   } else {
261     ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
262                                            payload);
263   }
264   if (payload) {
265     payload_type_map_[payload_number] = payload;
266   }
267   return ret_val;
268 }
269 
DeRegisterSendPayload(const int8_t payload_type)270 int32_t RTPSender::DeRegisterSendPayload(
271     const int8_t payload_type) {
272   CriticalSectionScoped lock(send_critsect_);
273 
274   std::map<int8_t, RtpUtility::Payload*>::iterator it =
275       payload_type_map_.find(payload_type);
276 
277   if (payload_type_map_.end() == it) {
278     return -1;
279   }
280   RtpUtility::Payload* payload = it->second;
281   delete payload;
282   payload_type_map_.erase(it);
283   return 0;
284 }
285 
SetSendPayloadType(int8_t payload_type)286 void RTPSender::SetSendPayloadType(int8_t payload_type) {
287   CriticalSectionScoped cs(send_critsect_);
288   payload_type_ = payload_type;
289 }
290 
SendPayloadType() const291 int8_t RTPSender::SendPayloadType() const {
292   CriticalSectionScoped cs(send_critsect_);
293   return payload_type_;
294 }
295 
SendPayloadFrequency() const296 int RTPSender::SendPayloadFrequency() const {
297   return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
298 }
299 
SetMaxPayloadLength(const uint16_t max_payload_length,const uint16_t packet_over_head)300 int32_t RTPSender::SetMaxPayloadLength(
301     const uint16_t max_payload_length,
302     const uint16_t packet_over_head) {
303   // Sanity check.
304   if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
305     LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
306     return -1;
307   }
308   CriticalSectionScoped cs(send_critsect_);
309   max_payload_length_ = max_payload_length;
310   packet_over_head_ = packet_over_head;
311   return 0;
312 }
313 
MaxDataPayloadLength() const314 uint16_t RTPSender::MaxDataPayloadLength() const {
315   int rtx;
316   {
317     CriticalSectionScoped rtx_lock(send_critsect_);
318     rtx = rtx_;
319   }
320   if (audio_configured_) {
321     return max_payload_length_ - RTPHeaderLength();
322   } else {
323     return max_payload_length_ - RTPHeaderLength()  // RTP overhead.
324            - video_->FECPacketOverhead()            // FEC/ULP/RED overhead.
325            - ((rtx) ? 2 : 0);                       // RTX overhead.
326   }
327 }
328 
MaxPayloadLength() const329 uint16_t RTPSender::MaxPayloadLength() const {
330   return max_payload_length_;
331 }
332 
PacketOverHead() const333 uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
334 
SetRTXStatus(int mode)335 void RTPSender::SetRTXStatus(int mode) {
336   CriticalSectionScoped cs(send_critsect_);
337   rtx_ = mode;
338 }
339 
SetRtxSsrc(uint32_t ssrc)340 void RTPSender::SetRtxSsrc(uint32_t ssrc) {
341   CriticalSectionScoped cs(send_critsect_);
342   ssrc_rtx_ = ssrc;
343 }
344 
RtxSsrc() const345 uint32_t RTPSender::RtxSsrc() const {
346   CriticalSectionScoped cs(send_critsect_);
347   return ssrc_rtx_;
348 }
349 
RTXStatus(int * mode,uint32_t * ssrc,int * payload_type) const350 void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
351                           int* payload_type) const {
352   CriticalSectionScoped cs(send_critsect_);
353   *mode = rtx_;
354   *ssrc = ssrc_rtx_;
355   *payload_type = payload_type_rtx_;
356 }
357 
SetRtxPayloadType(int payload_type)358 void RTPSender::SetRtxPayloadType(int payload_type) {
359   CriticalSectionScoped cs(send_critsect_);
360   payload_type_rtx_ = payload_type;
361 }
362 
CheckPayloadType(const int8_t payload_type,RtpVideoCodecTypes * video_type)363 int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
364                                     RtpVideoCodecTypes *video_type) {
365   CriticalSectionScoped cs(send_critsect_);
366 
367   if (payload_type < 0) {
368     LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
369     return -1;
370   }
371   if (audio_configured_) {
372     int8_t red_pl_type = -1;
373     if (audio_->RED(red_pl_type) == 0) {
374       // We have configured RED.
375       if (red_pl_type == payload_type) {
376         // And it's a match...
377         return 0;
378       }
379     }
380   }
381   if (payload_type_ == payload_type) {
382     if (!audio_configured_) {
383       *video_type = video_->VideoCodecType();
384     }
385     return 0;
386   }
387   std::map<int8_t, RtpUtility::Payload*>::iterator it =
388       payload_type_map_.find(payload_type);
389   if (it == payload_type_map_.end()) {
390     LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
391     return -1;
392   }
393   SetSendPayloadType(payload_type);
394   RtpUtility::Payload* payload = it->second;
395   assert(payload);
396   if (!payload->audio && !audio_configured_) {
397     video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
398     *video_type = payload->typeSpecific.Video.videoCodecType;
399     video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
400   }
401   return 0;
402 }
403 
SendOutgoingData(const FrameType frame_type,const int8_t payload_type,const uint32_t capture_timestamp,int64_t capture_time_ms,const uint8_t * payload_data,const uint32_t payload_size,const RTPFragmentationHeader * fragmentation,VideoCodecInformation * codec_info,const RTPVideoTypeHeader * rtp_type_hdr)404 int32_t RTPSender::SendOutgoingData(
405     const FrameType frame_type, const int8_t payload_type,
406     const uint32_t capture_timestamp, int64_t capture_time_ms,
407     const uint8_t *payload_data, const uint32_t payload_size,
408     const RTPFragmentationHeader *fragmentation,
409     VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
410   uint32_t ssrc;
411   {
412     // Drop this packet if we're not sending media packets.
413     CriticalSectionScoped cs(send_critsect_);
414     ssrc = ssrc_;
415     if (!sending_media_) {
416       return 0;
417     }
418   }
419   RtpVideoCodecTypes video_type = kRtpVideoGeneric;
420   if (CheckPayloadType(payload_type, &video_type) != 0) {
421     LOG(LS_ERROR) << "Don't send data with unknown payload type.";
422     return -1;
423   }
424 
425   uint32_t ret_val;
426   if (audio_configured_) {
427     TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
428                             "Send", "type", FrameTypeToString(frame_type));
429     assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
430            frame_type == kFrameEmpty);
431 
432     ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
433                                 payload_data, payload_size, fragmentation);
434   } else {
435     TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
436                             "Send", "type", FrameTypeToString(frame_type));
437     assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
438 
439     if (frame_type == kFrameEmpty)
440       return 0;
441 
442     ret_val = video_->SendVideo(video_type, frame_type, payload_type,
443                                 capture_timestamp, capture_time_ms,
444                                 payload_data, payload_size,
445                                 fragmentation, codec_info,
446                                 rtp_type_hdr);
447 
448   }
449 
450   CriticalSectionScoped cs(statistics_crit_.get());
451   uint32_t frame_count = ++frame_counts_[frame_type];
452   if (frame_count_observer_) {
453     frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
454   }
455 
456   return ret_val;
457 }
458 
TrySendRedundantPayloads(int bytes_to_send)459 int RTPSender::TrySendRedundantPayloads(int bytes_to_send) {
460   {
461     CriticalSectionScoped cs(send_critsect_);
462     if ((rtx_ & kRtxRedundantPayloads) == 0)
463       return 0;
464   }
465 
466   uint8_t buffer[IP_PACKET_SIZE];
467   int bytes_left = bytes_to_send;
468   while (bytes_left > 0) {
469     uint16_t length = bytes_left;
470     int64_t capture_time_ms;
471     if (!packet_history_.GetBestFittingPacket(buffer, &length,
472                                               &capture_time_ms)) {
473       break;
474     }
475     if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
476       return -1;
477     RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
478     RTPHeader rtp_header;
479     rtp_parser.Parse(rtp_header);
480     bytes_left -= length - rtp_header.headerLength;
481   }
482   return bytes_to_send - bytes_left;
483 }
484 
BuildPaddingPacket(uint8_t * packet,int header_length,int32_t bytes)485 int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
486                                   int32_t bytes) {
487   int padding_bytes_in_packet = kMaxPaddingLength;
488   if (bytes < kMaxPaddingLength) {
489     padding_bytes_in_packet = bytes;
490   }
491   packet[0] |= 0x20;  // Set padding bit.
492   int32_t *data =
493       reinterpret_cast<int32_t *>(&(packet[header_length]));
494 
495   // Fill data buffer with random data.
496   for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
497     data[j] = rand();  // NOLINT
498   }
499   // Set number of padding bytes in the last byte of the packet.
500   packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
501   return padding_bytes_in_packet;
502 }
503 
TrySendPadData(int bytes)504 int RTPSender::TrySendPadData(int bytes) {
505   int64_t capture_time_ms;
506   uint32_t timestamp;
507   {
508     CriticalSectionScoped cs(send_critsect_);
509     timestamp = timestamp_;
510     capture_time_ms = capture_time_ms_;
511     if (last_timestamp_time_ms_ > 0) {
512       timestamp +=
513           (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
514       capture_time_ms +=
515           (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
516     }
517   }
518   return SendPadData(timestamp, capture_time_ms, bytes);
519 }
520 
SendPadData(uint32_t timestamp,int64_t capture_time_ms,int32_t bytes)521 int RTPSender::SendPadData(uint32_t timestamp,
522                            int64_t capture_time_ms,
523                            int32_t bytes) {
524   int padding_bytes_in_packet = 0;
525   int bytes_sent = 0;
526   for (; bytes > 0; bytes -= padding_bytes_in_packet) {
527     // Always send full padding packets.
528     if (bytes < kMaxPaddingLength)
529       bytes = kMaxPaddingLength;
530 
531     uint32_t ssrc;
532     uint16_t sequence_number;
533     int payload_type;
534     bool over_rtx;
535     {
536       CriticalSectionScoped cs(send_critsect_);
537       // Only send padding packets following the last packet of a frame,
538       // indicated by the marker bit.
539       if (rtx_ == kRtxOff) {
540         // Without RTX we can't send padding in the middle of frames.
541         if (!last_packet_marker_bit_)
542           return 0;
543         ssrc = ssrc_;
544         sequence_number = sequence_number_;
545         ++sequence_number_;
546         payload_type = payload_type_;
547         over_rtx = false;
548       } else {
549         // Without abs-send-time a media packet must be sent before padding so
550         // that the timestamps used for estimation are correct.
551         if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
552             kRtpExtensionAbsoluteSendTime))
553           return 0;
554         ssrc = ssrc_rtx_;
555         sequence_number = sequence_number_rtx_;
556         ++sequence_number_rtx_;
557         payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
558                                                             : payload_type_;
559         over_rtx = true;
560       }
561     }
562 
563     uint8_t padding_packet[IP_PACKET_SIZE];
564     int header_length = CreateRTPHeader(padding_packet,
565                                         payload_type,
566                                         ssrc,
567                                         false,
568                                         timestamp,
569                                         sequence_number,
570                                         NULL,
571                                         0);
572     padding_bytes_in_packet =
573         BuildPaddingPacket(padding_packet, header_length, bytes);
574     int length = padding_bytes_in_packet + header_length;
575     int64_t now_ms = clock_->TimeInMilliseconds();
576 
577     RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
578     RTPHeader rtp_header;
579     rtp_parser.Parse(rtp_header);
580 
581     if (capture_time_ms > 0) {
582       UpdateTransmissionTimeOffset(
583           padding_packet, length, rtp_header, now_ms - capture_time_ms);
584     }
585 
586     UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
587     if (!SendPacketToNetwork(padding_packet, length))
588       break;
589     bytes_sent += padding_bytes_in_packet;
590     UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
591   }
592 
593   return bytes_sent;
594 }
595 
SetStorePacketsStatus(const bool enable,const uint16_t number_to_store)596 void RTPSender::SetStorePacketsStatus(const bool enable,
597                                       const uint16_t number_to_store) {
598   packet_history_.SetStorePacketsStatus(enable, number_to_store);
599 }
600 
StorePackets() const601 bool RTPSender::StorePackets() const {
602   return packet_history_.StorePackets();
603 }
604 
ReSendPacket(uint16_t packet_id,uint32_t min_resend_time)605 int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
606   uint16_t length = IP_PACKET_SIZE;
607   uint8_t data_buffer[IP_PACKET_SIZE];
608   int64_t capture_time_ms;
609   if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
610                                                data_buffer, &length,
611                                                &capture_time_ms)) {
612     // Packet not found.
613     return 0;
614   }
615 
616   if (paced_sender_) {
617     RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
618     RTPHeader header;
619     if (!rtp_parser.Parse(header)) {
620       assert(false);
621       return -1;
622     }
623     // Convert from TickTime to Clock since capture_time_ms is based on
624     // TickTime.
625     // TODO(holmer): Remove this conversion when we remove the use of TickTime.
626     int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
627         TickTime::MillisecondTimestamp();
628     if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
629                                    header.ssrc,
630                                    header.sequenceNumber,
631                                    capture_time_ms + clock_delta_ms,
632                                    length - header.headerLength,
633                                    true)) {
634       // We can't send the packet right now.
635       // We will be called when it is time.
636       return length;
637     }
638   }
639   int rtx = kRtxOff;
640   {
641     CriticalSectionScoped lock(send_critsect_);
642     rtx = rtx_;
643   }
644   return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
645                               (rtx & kRtxRetransmitted) > 0, true) ?
646       length : -1;
647 }
648 
SendPacketToNetwork(const uint8_t * packet,uint32_t size)649 bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
650   int bytes_sent = -1;
651   if (transport_) {
652     bytes_sent = transport_->SendPacket(id_, packet, size);
653   }
654   TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
655                        "size", size, "sent", bytes_sent);
656   // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
657   if (bytes_sent <= 0) {
658     LOG(LS_WARNING) << "Transport failed to send packet";
659     return false;
660   }
661   return true;
662 }
663 
SelectiveRetransmissions() const664 int RTPSender::SelectiveRetransmissions() const {
665   if (!video_)
666     return -1;
667   return video_->SelectiveRetransmissions();
668 }
669 
SetSelectiveRetransmissions(uint8_t settings)670 int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
671   if (!video_)
672     return -1;
673   return video_->SetSelectiveRetransmissions(settings);
674 }
675 
OnReceivedNACK(const std::list<uint16_t> & nack_sequence_numbers,const uint16_t avg_rtt)676 void RTPSender::OnReceivedNACK(
677     const std::list<uint16_t>& nack_sequence_numbers,
678     const uint16_t avg_rtt) {
679   TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
680                "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
681   const int64_t now = clock_->TimeInMilliseconds();
682   uint32_t bytes_re_sent = 0;
683   uint32_t target_bitrate = GetTargetBitrate();
684 
685   // Enough bandwidth to send NACK?
686   if (!ProcessNACKBitRate(now)) {
687     LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
688                  << target_bitrate;
689     return;
690   }
691 
692   for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
693       it != nack_sequence_numbers.end(); ++it) {
694     const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
695     if (bytes_sent > 0) {
696       bytes_re_sent += bytes_sent;
697     } else if (bytes_sent == 0) {
698       // The packet has previously been resent.
699       // Try resending next packet in the list.
700       continue;
701     } else if (bytes_sent < 0) {
702       // Failed to send one Sequence number. Give up the rest in this nack.
703       LOG(LS_WARNING) << "Failed resending RTP packet " << *it
704                       << ", Discard rest of packets";
705       break;
706     }
707     // Delay bandwidth estimate (RTT * BW).
708     if (target_bitrate != 0 && avg_rtt) {
709       // kbits/s * ms = bits => bits/8 = bytes
710       uint32_t target_bytes =
711           (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
712       if (bytes_re_sent > target_bytes) {
713         break;  // Ignore the rest of the packets in the list.
714       }
715     }
716   }
717   if (bytes_re_sent > 0) {
718     // TODO(pwestin) consolidate these two methods.
719     UpdateNACKBitRate(bytes_re_sent, now);
720     nack_bitrate_.Update(bytes_re_sent);
721   }
722 }
723 
ProcessNACKBitRate(const uint32_t now)724 bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
725   uint32_t num = 0;
726   int byte_count = 0;
727   const uint32_t kAvgIntervalMs = 1000;
728   uint32_t target_bitrate = GetTargetBitrate();
729 
730   CriticalSectionScoped cs(send_critsect_);
731 
732   if (target_bitrate == 0) {
733     return true;
734   }
735   for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
736     if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
737       // Don't use data older than 1sec.
738       break;
739     } else {
740       byte_count += nack_byte_count_[num];
741     }
742   }
743   uint32_t time_interval = kAvgIntervalMs;
744   if (num == NACK_BYTECOUNT_SIZE) {
745     // More than NACK_BYTECOUNT_SIZE nack messages has been received
746     // during the last msg_interval.
747     if (nack_byte_count_times_[num - 1] <= now) {
748       time_interval = now - nack_byte_count_times_[num - 1];
749     }
750   }
751   return (byte_count * 8) <
752          static_cast<int>(target_bitrate / 1000 * time_interval);
753 }
754 
UpdateNACKBitRate(const uint32_t bytes,const uint32_t now)755 void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
756                                   const uint32_t now) {
757   CriticalSectionScoped cs(send_critsect_);
758 
759   // Save bitrate statistics.
760   if (bytes > 0) {
761     if (now == 0) {
762       // Add padding length.
763       nack_byte_count_[0] += bytes;
764     } else {
765       if (nack_byte_count_times_[0] == 0) {
766         // First no shift.
767       } else {
768         // Shift.
769         for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
770           nack_byte_count_[i + 1] = nack_byte_count_[i];
771           nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
772         }
773       }
774       nack_byte_count_[0] = bytes;
775       nack_byte_count_times_[0] = now;
776     }
777   }
778 }
779 
780 // Called from pacer when we can send the packet.
TimeToSendPacket(uint16_t sequence_number,int64_t capture_time_ms,bool retransmission)781 bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
782                                  int64_t capture_time_ms,
783                                  bool retransmission) {
784   uint16_t length = IP_PACKET_SIZE;
785   uint8_t data_buffer[IP_PACKET_SIZE];
786   int64_t stored_time_ms;
787 
788   if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
789                                                0,
790                                                retransmission,
791                                                data_buffer,
792                                                &length,
793                                                &stored_time_ms)) {
794     // Packet cannot be found. Allow sending to continue.
795     return true;
796   }
797   if (!retransmission && capture_time_ms > 0) {
798     UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
799   }
800   int rtx;
801   {
802     CriticalSectionScoped lock(send_critsect_);
803     rtx = rtx_;
804   }
805   return PrepareAndSendPacket(data_buffer,
806                               length,
807                               capture_time_ms,
808                               retransmission && (rtx & kRtxRetransmitted) > 0,
809                               retransmission);
810 }
811 
PrepareAndSendPacket(uint8_t * buffer,uint16_t length,int64_t capture_time_ms,bool send_over_rtx,bool is_retransmit)812 bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
813                                      uint16_t length,
814                                      int64_t capture_time_ms,
815                                      bool send_over_rtx,
816                                      bool is_retransmit) {
817   uint8_t *buffer_to_send_ptr = buffer;
818 
819   RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
820   RTPHeader rtp_header;
821   rtp_parser.Parse(rtp_header);
822   TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
823                        "timestamp", rtp_header.timestamp,
824                        "seqnum", rtp_header.sequenceNumber);
825 
826   uint8_t data_buffer_rtx[IP_PACKET_SIZE];
827   if (send_over_rtx) {
828     BuildRtxPacket(buffer, &length, data_buffer_rtx);
829     buffer_to_send_ptr = data_buffer_rtx;
830   }
831 
832   int64_t now_ms = clock_->TimeInMilliseconds();
833   int64_t diff_ms = now_ms - capture_time_ms;
834   UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
835                                diff_ms);
836   UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
837   bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
838   if (ret) {
839     CriticalSectionScoped lock(send_critsect_);
840     media_has_been_sent_ = true;
841   }
842   UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
843                  is_retransmit);
844   return ret;
845 }
846 
UpdateRtpStats(const uint8_t * buffer,uint32_t size,const RTPHeader & header,bool is_rtx,bool is_retransmit)847 void RTPSender::UpdateRtpStats(const uint8_t* buffer,
848                                uint32_t size,
849                                const RTPHeader& header,
850                                bool is_rtx,
851                                bool is_retransmit) {
852   StreamDataCounters* counters;
853   // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
854   uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
855 
856   CriticalSectionScoped lock(statistics_crit_.get());
857   if (is_rtx) {
858     counters = &rtx_rtp_stats_;
859   } else {
860     counters = &rtp_stats_;
861   }
862 
863   bitrate_sent_.Update(size);
864   ++counters->packets;
865   if (IsFecPacket(buffer, header)) {
866     ++counters->fec_packets;
867   }
868 
869   if (is_retransmit) {
870     ++counters->retransmitted_packets;
871   } else {
872     counters->bytes += size - (header.headerLength + header.paddingLength);
873     counters->header_bytes += header.headerLength;
874     counters->padding_bytes += header.paddingLength;
875   }
876 
877   if (rtp_stats_callback_) {
878     rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
879   }
880 }
881 
IsFecPacket(const uint8_t * buffer,const RTPHeader & header) const882 bool RTPSender::IsFecPacket(const uint8_t* buffer,
883                             const RTPHeader& header) const {
884   if (!video_) {
885     return false;
886   }
887   bool fec_enabled;
888   uint8_t pt_red;
889   uint8_t pt_fec;
890   video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
891   return fec_enabled &&
892       header.payloadType == pt_red &&
893       buffer[header.headerLength] == pt_fec;
894 }
895 
TimeToSendPadding(int bytes)896 int RTPSender::TimeToSendPadding(int bytes) {
897   {
898     CriticalSectionScoped cs(send_critsect_);
899     if (!sending_media_) return 0;
900   }
901   int available_bytes = bytes;
902   if (available_bytes > 0)
903     available_bytes -= TrySendRedundantPayloads(available_bytes);
904   if (available_bytes > 0)
905     available_bytes -= TrySendPadData(available_bytes);
906   return bytes - available_bytes;
907 }
908 
909 // TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
SendToNetwork(uint8_t * buffer,int payload_length,int rtp_header_length,int64_t capture_time_ms,StorageType storage,PacedSender::Priority priority)910 int32_t RTPSender::SendToNetwork(
911     uint8_t *buffer, int payload_length, int rtp_header_length,
912     int64_t capture_time_ms, StorageType storage,
913     PacedSender::Priority priority) {
914   RtpUtility::RtpHeaderParser rtp_parser(buffer,
915                                          payload_length + rtp_header_length);
916   RTPHeader rtp_header;
917   rtp_parser.Parse(rtp_header);
918 
919   int64_t now_ms = clock_->TimeInMilliseconds();
920 
921   // |capture_time_ms| <= 0 is considered invalid.
922   // TODO(holmer): This should be changed all over Video Engine so that negative
923   // time is consider invalid, while 0 is considered a valid time.
924   if (capture_time_ms > 0) {
925     UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
926                                  rtp_header, now_ms - capture_time_ms);
927   }
928 
929   UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
930                          rtp_header, now_ms);
931 
932   // Used for NACK and to spread out the transmission of packets.
933   if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
934                                    max_payload_length_, capture_time_ms,
935                                    storage) != 0) {
936     return -1;
937   }
938 
939   if (paced_sender_ && storage != kDontStore) {
940     int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
941         TickTime::MillisecondTimestamp();
942     if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
943                                    rtp_header.sequenceNumber,
944                                    capture_time_ms + clock_delta_ms,
945                                    payload_length, false)) {
946       // We can't send the packet right now.
947       // We will be called when it is time.
948       return 0;
949     }
950   }
951   if (capture_time_ms > 0) {
952     UpdateDelayStatistics(capture_time_ms, now_ms);
953   }
954   uint32_t length = payload_length + rtp_header_length;
955   if (!SendPacketToNetwork(buffer, length))
956     return -1;
957   assert(payload_length - rtp_header.paddingLength > 0);
958   {
959     CriticalSectionScoped lock(send_critsect_);
960     media_has_been_sent_ = true;
961   }
962   UpdateRtpStats(buffer, length, rtp_header, false, false);
963   return 0;
964 }
965 
UpdateDelayStatistics(int64_t capture_time_ms,int64_t now_ms)966 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
967   uint32_t ssrc;
968   int avg_delay_ms = 0;
969   int max_delay_ms = 0;
970   {
971     CriticalSectionScoped lock(send_critsect_);
972     ssrc = ssrc_;
973   }
974   {
975     CriticalSectionScoped cs(statistics_crit_.get());
976     // TODO(holmer): Compute this iteratively instead.
977     send_delays_[now_ms] = now_ms - capture_time_ms;
978     send_delays_.erase(send_delays_.begin(),
979                        send_delays_.lower_bound(now_ms -
980                        kSendSideDelayWindowMs));
981   }
982   if (send_side_delay_observer_ &&
983       GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
984     send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
985         max_delay_ms, ssrc);
986   }
987 }
988 
ProcessBitrate()989 void RTPSender::ProcessBitrate() {
990   CriticalSectionScoped cs(send_critsect_);
991   bitrate_sent_.Process();
992   nack_bitrate_.Process();
993   if (audio_configured_) {
994     return;
995   }
996   video_->ProcessBitrate();
997 }
998 
RTPHeaderLength() const999 uint16_t RTPSender::RTPHeaderLength() const {
1000   CriticalSectionScoped lock(send_critsect_);
1001   uint16_t rtp_header_length = 12;
1002   if (include_csrcs_) {
1003     rtp_header_length += sizeof(uint32_t) * num_csrcs_;
1004   }
1005   rtp_header_length += RtpHeaderExtensionTotalLength();
1006   return rtp_header_length;
1007 }
1008 
IncrementSequenceNumber()1009 uint16_t RTPSender::IncrementSequenceNumber() {
1010   CriticalSectionScoped cs(send_critsect_);
1011   return sequence_number_++;
1012 }
1013 
ResetDataCounters()1014 void RTPSender::ResetDataCounters() {
1015   uint32_t ssrc;
1016   uint32_t ssrc_rtx;
1017   {
1018     CriticalSectionScoped ssrc_lock(send_critsect_);
1019     ssrc = ssrc_;
1020     ssrc_rtx = ssrc_rtx_;
1021   }
1022   CriticalSectionScoped lock(statistics_crit_.get());
1023   rtp_stats_ = StreamDataCounters();
1024   rtx_rtp_stats_ = StreamDataCounters();
1025   if (rtp_stats_callback_) {
1026     rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1027     rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
1028   }
1029 }
1030 
GetDataCounters(StreamDataCounters * rtp_stats,StreamDataCounters * rtx_stats) const1031 void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1032                                 StreamDataCounters* rtx_stats) const {
1033   CriticalSectionScoped lock(statistics_crit_.get());
1034   *rtp_stats = rtp_stats_;
1035   *rtx_stats = rtx_rtp_stats_;
1036 }
1037 
CreateRTPHeader(uint8_t * header,int8_t payload_type,uint32_t ssrc,bool marker_bit,uint32_t timestamp,uint16_t sequence_number,const uint32_t * csrcs,uint8_t num_csrcs) const1038 int RTPSender::CreateRTPHeader(
1039     uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1040     uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1041     uint8_t num_csrcs) const {
1042   header[0] = 0x80;  // version 2.
1043   header[1] = static_cast<uint8_t>(payload_type);
1044   if (marker_bit) {
1045     header[1] |= kRtpMarkerBitMask;  // Marker bit is set.
1046   }
1047   RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1048   RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1049   RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
1050   int32_t rtp_header_length = 12;
1051 
1052   // Add the CSRCs if any.
1053   if (num_csrcs > 0) {
1054     if (num_csrcs > kRtpCsrcSize) {
1055       // error
1056       assert(false);
1057       return -1;
1058     }
1059     uint8_t *ptr = &header[rtp_header_length];
1060     for (int i = 0; i < num_csrcs; ++i) {
1061       RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
1062       ptr += 4;
1063     }
1064     header[0] = (header[0] & 0xf0) | num_csrcs;
1065 
1066     // Update length of header.
1067     rtp_header_length += sizeof(uint32_t) * num_csrcs;
1068   }
1069 
1070   uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1071   if (len > 0) {
1072     header[0] |= 0x10;  // Set extension bit.
1073     rtp_header_length += len;
1074   }
1075   return rtp_header_length;
1076 }
1077 
BuildRTPheader(uint8_t * data_buffer,const int8_t payload_type,const bool marker_bit,const uint32_t capture_timestamp,int64_t capture_time_ms,const bool timestamp_provided,const bool inc_sequence_number)1078 int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1079                                   const int8_t payload_type,
1080                                   const bool marker_bit,
1081                                   const uint32_t capture_timestamp,
1082                                   int64_t capture_time_ms,
1083                                   const bool timestamp_provided,
1084                                   const bool inc_sequence_number) {
1085   assert(payload_type >= 0);
1086   CriticalSectionScoped cs(send_critsect_);
1087 
1088   if (timestamp_provided) {
1089     timestamp_ = start_timestamp_ + capture_timestamp;
1090   } else {
1091     // Make a unique time stamp.
1092     // We can't inc by the actual time, since then we increase the risk of back
1093     // timing.
1094     timestamp_++;
1095   }
1096   last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
1097   uint32_t sequence_number = sequence_number_++;
1098   capture_time_ms_ = capture_time_ms;
1099   last_packet_marker_bit_ = marker_bit;
1100   int csrcs_length = 0;
1101   if (include_csrcs_)
1102     csrcs_length = num_csrcs_;
1103   return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1104                          timestamp_, sequence_number, csrcs_, csrcs_length);
1105 }
1106 
BuildRTPHeaderExtension(uint8_t * data_buffer) const1107 uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
1108   if (rtp_header_extension_map_.Size() <= 0) {
1109     return 0;
1110   }
1111   // RTP header extension, RFC 3550.
1112   //   0                   1                   2                   3
1113   //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1114   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1115   //  |      defined by profile       |           length              |
1116   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1117   //  |                        header extension                       |
1118   //  |                             ....                              |
1119   //
1120   const uint32_t kPosLength = 2;
1121   const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
1122 
1123   // Add extension ID (0xBEDE).
1124   RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
1125 
1126   // Add extensions.
1127   uint16_t total_block_length = 0;
1128 
1129   RTPExtensionType type = rtp_header_extension_map_.First();
1130   while (type != kRtpExtensionNone) {
1131     uint8_t block_length = 0;
1132     switch (type) {
1133       case kRtpExtensionTransmissionTimeOffset:
1134         block_length = BuildTransmissionTimeOffsetExtension(
1135             data_buffer + kHeaderLength + total_block_length);
1136         break;
1137       case kRtpExtensionAudioLevel:
1138         block_length = BuildAudioLevelExtension(
1139             data_buffer + kHeaderLength + total_block_length);
1140         break;
1141       case kRtpExtensionAbsoluteSendTime:
1142         block_length = BuildAbsoluteSendTimeExtension(
1143             data_buffer + kHeaderLength + total_block_length);
1144         break;
1145       default:
1146         assert(false);
1147     }
1148     total_block_length += block_length;
1149     type = rtp_header_extension_map_.Next(type);
1150   }
1151   if (total_block_length == 0) {
1152     // No extension added.
1153     return 0;
1154   }
1155   // Set header length (in number of Word32, header excluded).
1156   assert(total_block_length % 4 == 0);
1157   RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1158                                     total_block_length / 4);
1159   // Total added length.
1160   return kHeaderLength + total_block_length;
1161 }
1162 
BuildTransmissionTimeOffsetExtension(uint8_t * data_buffer) const1163 uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1164     uint8_t* data_buffer) const {
1165   // From RFC 5450: Transmission Time Offsets in RTP Streams.
1166   //
1167   // The transmission time is signaled to the receiver in-band using the
1168   // general mechanism for RTP header extensions [RFC5285]. The payload
1169   // of this extension (the transmitted value) is a 24-bit signed integer.
1170   // When added to the RTP timestamp of the packet, it represents the
1171   // "effective" RTP transmission time of the packet, on the RTP
1172   // timescale.
1173   //
1174   // The form of the transmission offset extension block:
1175   //
1176   //    0                   1                   2                   3
1177   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1178   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1179   //   |  ID   | len=2 |              transmission offset              |
1180   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1181 
1182   // Get id defined by user.
1183   uint8_t id;
1184   if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1185                                       &id) != 0) {
1186     // Not registered.
1187     return 0;
1188   }
1189   size_t pos = 0;
1190   const uint8_t len = 2;
1191   data_buffer[pos++] = (id << 4) + len;
1192   RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1193                                     transmission_time_offset_);
1194   pos += 3;
1195   assert(pos == kTransmissionTimeOffsetLength);
1196   return kTransmissionTimeOffsetLength;
1197 }
1198 
BuildAudioLevelExtension(uint8_t * data_buffer) const1199 uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1200   // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1201   //
1202   // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1203   //
1204   // The form of the audio level extension block:
1205   //
1206   //    0                   1                   2                   3
1207   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1208   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1209   //    |  ID   | len=0 |V|   level     |      0x00     |      0x00     |
1210   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1211   //
1212   // Note that we always include 2 pad bytes, which will result in legal and
1213   // correctly parsed RTP, but may be a bit wasteful if more short extensions
1214   // are implemented. Right now the pad bytes would anyway be required at end
1215   // of the extension block, so it makes no difference.
1216 
1217   // Get id defined by user.
1218   uint8_t id;
1219   if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1220     // Not registered.
1221     return 0;
1222   }
1223   size_t pos = 0;
1224   const uint8_t len = 0;
1225   data_buffer[pos++] = (id << 4) + len;
1226   data_buffer[pos++] = (1 << 7) + 0;     // Voice, 0 dBov.
1227   data_buffer[pos++] = 0;                // Padding.
1228   data_buffer[pos++] = 0;                // Padding.
1229   // kAudioLevelLength is including pad bytes.
1230   assert(pos == kAudioLevelLength);
1231   return kAudioLevelLength;
1232 }
1233 
BuildAbsoluteSendTimeExtension(uint8_t * data_buffer) const1234 uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
1235   // Absolute send time in RTP streams.
1236   //
1237   // The absolute send time is signaled to the receiver in-band using the
1238   // general mechanism for RTP header extensions [RFC5285]. The payload
1239   // of this extension (the transmitted value) is a 24-bit unsigned integer
1240   // containing the sender's current time in seconds as a fixed point number
1241   // with 18 bits fractional part.
1242   //
1243   // The form of the absolute send time extension block:
1244   //
1245   //    0                   1                   2                   3
1246   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1247   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1248   //   |  ID   | len=2 |              absolute send time               |
1249   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1250 
1251   // Get id defined by user.
1252   uint8_t id;
1253   if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1254                                       &id) != 0) {
1255     // Not registered.
1256     return 0;
1257   }
1258   size_t pos = 0;
1259   const uint8_t len = 2;
1260   data_buffer[pos++] = (id << 4) + len;
1261   RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
1262   pos += 3;
1263   assert(pos == kAbsoluteSendTimeLength);
1264   return kAbsoluteSendTimeLength;
1265 }
1266 
UpdateTransmissionTimeOffset(uint8_t * rtp_packet,const uint16_t rtp_packet_length,const RTPHeader & rtp_header,const int64_t time_diff_ms) const1267 void RTPSender::UpdateTransmissionTimeOffset(
1268     uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1269     const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
1270   CriticalSectionScoped cs(send_critsect_);
1271   // Get id.
1272   uint8_t id = 0;
1273   if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1274                                       &id) != 0) {
1275     // Not registered.
1276     return;
1277   }
1278   // Get length until start of header extension block.
1279   int extension_block_pos =
1280       rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1281           kRtpExtensionTransmissionTimeOffset);
1282   if (extension_block_pos < 0) {
1283     LOG(LS_WARNING)
1284         << "Failed to update transmission time offset, not registered.";
1285     return;
1286   }
1287   int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1288   if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
1289       rtp_header.headerLength <
1290           block_pos + kTransmissionTimeOffsetLength) {
1291     LOG(LS_WARNING)
1292         << "Failed to update transmission time offset, invalid length.";
1293     return;
1294   }
1295   // Verify that header contains extension.
1296   if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1297         (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1298     LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1299                        "extension not found.";
1300     return;
1301   }
1302   // Verify first byte in block.
1303   const uint8_t first_block_byte = (id << 4) + 2;
1304   if (rtp_packet[block_pos] != first_block_byte) {
1305     LOG(LS_WARNING) << "Failed to update transmission time offset.";
1306     return;
1307   }
1308   // Update transmission offset field (converting to a 90 kHz timestamp).
1309   RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1310                                     time_diff_ms * 90);  // RTP timestamp.
1311 }
1312 
UpdateAudioLevel(uint8_t * rtp_packet,const uint16_t rtp_packet_length,const RTPHeader & rtp_header,const bool is_voiced,const uint8_t dBov) const1313 bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1314                                  const uint16_t rtp_packet_length,
1315                                  const RTPHeader &rtp_header,
1316                                  const bool is_voiced,
1317                                  const uint8_t dBov) const {
1318   CriticalSectionScoped cs(send_critsect_);
1319 
1320   // Get id.
1321   uint8_t id = 0;
1322   if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1323     // Not registered.
1324     return false;
1325   }
1326   // Get length until start of header extension block.
1327   int extension_block_pos =
1328       rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1329           kRtpExtensionAudioLevel);
1330   if (extension_block_pos < 0) {
1331     // The feature is not enabled.
1332     return false;
1333   }
1334   int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1335   if (rtp_packet_length < block_pos + kAudioLevelLength ||
1336       rtp_header.headerLength < block_pos + kAudioLevelLength) {
1337     LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
1338     return false;
1339   }
1340   // Verify that header contains extension.
1341   if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1342         (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1343     LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
1344     return false;
1345   }
1346   // Verify first byte in block.
1347   const uint8_t first_block_byte = (id << 4) + 0;
1348   if (rtp_packet[block_pos] != first_block_byte) {
1349     LOG(LS_WARNING) << "Failed to update audio level.";
1350     return false;
1351   }
1352   rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1353   return true;
1354 }
1355 
UpdateAbsoluteSendTime(uint8_t * rtp_packet,const uint16_t rtp_packet_length,const RTPHeader & rtp_header,const int64_t now_ms) const1356 void RTPSender::UpdateAbsoluteSendTime(
1357     uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1358     const RTPHeader &rtp_header, const int64_t now_ms) const {
1359   CriticalSectionScoped cs(send_critsect_);
1360 
1361   // Get id.
1362   uint8_t id = 0;
1363   if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1364                                       &id) != 0) {
1365     // Not registered.
1366     return;
1367   }
1368   // Get length until start of header extension block.
1369   int extension_block_pos =
1370       rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1371           kRtpExtensionAbsoluteSendTime);
1372   if (extension_block_pos < 0) {
1373     // The feature is not enabled.
1374     return;
1375   }
1376   int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1377   if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
1378       rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
1379     LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
1380     return;
1381   }
1382   // Verify that header contains extension.
1383   if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1384         (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1385     LOG(LS_WARNING)
1386         << "Failed to update absolute send time, hdr extension not found.";
1387     return;
1388   }
1389   // Verify first byte in block.
1390   const uint8_t first_block_byte = (id << 4) + 2;
1391   if (rtp_packet[block_pos] != first_block_byte) {
1392     LOG(LS_WARNING) << "Failed to update absolute send time.";
1393     return;
1394   }
1395   // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1396   // fractional part).
1397   RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1398                                     ((now_ms << 18) / 1000) & 0x00ffffff);
1399 }
1400 
SetSendingStatus(bool enabled)1401 void RTPSender::SetSendingStatus(bool enabled) {
1402   if (enabled) {
1403     uint32_t frequency_hz = SendPayloadFrequency();
1404     uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
1405 
1406     // Will be ignored if it's already configured via API.
1407     SetStartTimestamp(RTPtime, false);
1408   } else {
1409     CriticalSectionScoped lock(send_critsect_);
1410     if (!ssrc_forced_) {
1411       // Generate a new SSRC.
1412       ssrc_db_.ReturnSSRC(ssrc_);
1413       ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
1414     }
1415     // Don't initialize seq number if SSRC passed externally.
1416     if (!sequence_number_forced_ && !ssrc_forced_) {
1417       // Generate a new sequence number.
1418       sequence_number_ =
1419           rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER);  // NOLINT
1420     }
1421   }
1422 }
1423 
SetSendingMediaStatus(const bool enabled)1424 void RTPSender::SetSendingMediaStatus(const bool enabled) {
1425   CriticalSectionScoped cs(send_critsect_);
1426   sending_media_ = enabled;
1427 }
1428 
SendingMedia() const1429 bool RTPSender::SendingMedia() const {
1430   CriticalSectionScoped cs(send_critsect_);
1431   return sending_media_;
1432 }
1433 
Timestamp() const1434 uint32_t RTPSender::Timestamp() const {
1435   CriticalSectionScoped cs(send_critsect_);
1436   return timestamp_;
1437 }
1438 
SetStartTimestamp(uint32_t timestamp,bool force)1439 void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
1440   CriticalSectionScoped cs(send_critsect_);
1441   if (force) {
1442     start_timestamp_forced_ = true;
1443     start_timestamp_ = timestamp;
1444   } else {
1445     if (!start_timestamp_forced_) {
1446       start_timestamp_ = timestamp;
1447     }
1448   }
1449 }
1450 
StartTimestamp() const1451 uint32_t RTPSender::StartTimestamp() const {
1452   CriticalSectionScoped cs(send_critsect_);
1453   return start_timestamp_;
1454 }
1455 
GenerateNewSSRC()1456 uint32_t RTPSender::GenerateNewSSRC() {
1457   // If configured via API, return 0.
1458   CriticalSectionScoped cs(send_critsect_);
1459 
1460   if (ssrc_forced_) {
1461     return 0;
1462   }
1463   ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
1464   return ssrc_;
1465 }
1466 
SetSSRC(uint32_t ssrc)1467 void RTPSender::SetSSRC(uint32_t ssrc) {
1468   // This is configured via the API.
1469   CriticalSectionScoped cs(send_critsect_);
1470 
1471   if (ssrc_ == ssrc && ssrc_forced_) {
1472     return;  // Since it's same ssrc, don't reset anything.
1473   }
1474   ssrc_forced_ = true;
1475   ssrc_db_.ReturnSSRC(ssrc_);
1476   ssrc_db_.RegisterSSRC(ssrc);
1477   ssrc_ = ssrc;
1478   if (!sequence_number_forced_) {
1479     sequence_number_ =
1480         rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER);  // NOLINT
1481   }
1482 }
1483 
SSRC() const1484 uint32_t RTPSender::SSRC() const {
1485   CriticalSectionScoped cs(send_critsect_);
1486   return ssrc_;
1487 }
1488 
SetCSRCStatus(const bool include)1489 void RTPSender::SetCSRCStatus(const bool include) {
1490   CriticalSectionScoped lock(send_critsect_);
1491   include_csrcs_ = include;
1492 }
1493 
SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],const uint8_t arr_length)1494 void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1495                          const uint8_t arr_length) {
1496   assert(arr_length <= kRtpCsrcSize);
1497   CriticalSectionScoped cs(send_critsect_);
1498 
1499   for (int i = 0; i < arr_length; i++) {
1500     csrcs_[i] = arr_of_csrc[i];
1501   }
1502   num_csrcs_ = arr_length;
1503 }
1504 
CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const1505 int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
1506   assert(arr_of_csrc);
1507   CriticalSectionScoped cs(send_critsect_);
1508   for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1509     arr_of_csrc[i] = csrcs_[i];
1510   }
1511   return num_csrcs_;
1512 }
1513 
SetSequenceNumber(uint16_t seq)1514 void RTPSender::SetSequenceNumber(uint16_t seq) {
1515   CriticalSectionScoped cs(send_critsect_);
1516   sequence_number_forced_ = true;
1517   sequence_number_ = seq;
1518 }
1519 
SequenceNumber() const1520 uint16_t RTPSender::SequenceNumber() const {
1521   CriticalSectionScoped cs(send_critsect_);
1522   return sequence_number_;
1523 }
1524 
1525 // Audio.
SendTelephoneEvent(const uint8_t key,const uint16_t time_ms,const uint8_t level)1526 int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1527                                       const uint16_t time_ms,
1528                                       const uint8_t level) {
1529   if (!audio_configured_) {
1530     return -1;
1531   }
1532   return audio_->SendTelephoneEvent(key, time_ms, level);
1533 }
1534 
SendTelephoneEventActive(int8_t * telephone_event) const1535 bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
1536   if (!audio_configured_) {
1537     return false;
1538   }
1539   return audio_->SendTelephoneEventActive(*telephone_event);
1540 }
1541 
SetAudioPacketSize(const uint16_t packet_size_samples)1542 int32_t RTPSender::SetAudioPacketSize(
1543     const uint16_t packet_size_samples) {
1544   if (!audio_configured_) {
1545     return -1;
1546   }
1547   return audio_->SetAudioPacketSize(packet_size_samples);
1548 }
1549 
SetAudioLevel(const uint8_t level_d_bov)1550 int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
1551   return audio_->SetAudioLevel(level_d_bov);
1552 }
1553 
SetRED(const int8_t payload_type)1554 int32_t RTPSender::SetRED(const int8_t payload_type) {
1555   if (!audio_configured_) {
1556     return -1;
1557   }
1558   return audio_->SetRED(payload_type);
1559 }
1560 
RED(int8_t * payload_type) const1561 int32_t RTPSender::RED(int8_t *payload_type) const {
1562   if (!audio_configured_) {
1563     return -1;
1564   }
1565   return audio_->RED(*payload_type);
1566 }
1567 
1568 // Video
CodecInformationVideo()1569 VideoCodecInformation *RTPSender::CodecInformationVideo() {
1570   if (audio_configured_) {
1571     return NULL;
1572   }
1573   return video_->CodecInformationVideo();
1574 }
1575 
VideoCodecType() const1576 RtpVideoCodecTypes RTPSender::VideoCodecType() const {
1577   assert(!audio_configured_ && "Sender is an audio stream!");
1578   return video_->VideoCodecType();
1579 }
1580 
MaxConfiguredBitrateVideo() const1581 uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
1582   if (audio_configured_) {
1583     return 0;
1584   }
1585   return video_->MaxConfiguredBitrateVideo();
1586 }
1587 
SendRTPIntraRequest()1588 int32_t RTPSender::SendRTPIntraRequest() {
1589   if (audio_configured_) {
1590     return -1;
1591   }
1592   return video_->SendRTPIntraRequest();
1593 }
1594 
SetGenericFECStatus(const bool enable,const uint8_t payload_type_red,const uint8_t payload_type_fec)1595 int32_t RTPSender::SetGenericFECStatus(
1596     const bool enable, const uint8_t payload_type_red,
1597     const uint8_t payload_type_fec) {
1598   if (audio_configured_) {
1599     return -1;
1600   }
1601   return video_->SetGenericFECStatus(enable, payload_type_red,
1602                                      payload_type_fec);
1603 }
1604 
GenericFECStatus(bool * enable,uint8_t * payload_type_red,uint8_t * payload_type_fec) const1605 int32_t RTPSender::GenericFECStatus(
1606     bool *enable, uint8_t *payload_type_red,
1607     uint8_t *payload_type_fec) const {
1608   if (audio_configured_) {
1609     return -1;
1610   }
1611   return video_->GenericFECStatus(
1612       *enable, *payload_type_red, *payload_type_fec);
1613 }
1614 
SetFecParameters(const FecProtectionParams * delta_params,const FecProtectionParams * key_params)1615 int32_t RTPSender::SetFecParameters(
1616     const FecProtectionParams *delta_params,
1617     const FecProtectionParams *key_params) {
1618   if (audio_configured_) {
1619     return -1;
1620   }
1621   return video_->SetFecParameters(delta_params, key_params);
1622 }
1623 
BuildRtxPacket(uint8_t * buffer,uint16_t * length,uint8_t * buffer_rtx)1624 void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1625                                uint8_t* buffer_rtx) {
1626   CriticalSectionScoped cs(send_critsect_);
1627   uint8_t* data_buffer_rtx = buffer_rtx;
1628   // Add RTX header.
1629   RtpUtility::RtpHeaderParser rtp_parser(
1630       reinterpret_cast<const uint8_t*>(buffer), *length);
1631 
1632   RTPHeader rtp_header;
1633   rtp_parser.Parse(rtp_header);
1634 
1635   // Add original RTP header.
1636   memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
1637 
1638   // Replace payload type, if a specific type is set for RTX.
1639   if (payload_type_rtx_ != -1) {
1640     data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
1641     if (rtp_header.markerBit)
1642       data_buffer_rtx[1] |= kRtpMarkerBitMask;
1643   }
1644 
1645   // Replace sequence number.
1646   uint8_t *ptr = data_buffer_rtx + 2;
1647   RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1648 
1649   // Replace SSRC.
1650   ptr += 6;
1651   RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1652 
1653   // Add OSN (original sequence number).
1654   ptr = data_buffer_rtx + rtp_header.headerLength;
1655   RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
1656   ptr += 2;
1657 
1658   // Add original payload data.
1659   memcpy(ptr, buffer + rtp_header.headerLength,
1660          *length - rtp_header.headerLength);
1661   *length += 2;
1662 }
1663 
RegisterRtpStatisticsCallback(StreamDataCountersCallback * callback)1664 void RTPSender::RegisterRtpStatisticsCallback(
1665     StreamDataCountersCallback* callback) {
1666   CriticalSectionScoped cs(statistics_crit_.get());
1667   rtp_stats_callback_ = callback;
1668 }
1669 
GetRtpStatisticsCallback() const1670 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1671   CriticalSectionScoped cs(statistics_crit_.get());
1672   return rtp_stats_callback_;
1673 }
1674 
BitrateSent() const1675 uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1676 
BitrateUpdated(const BitrateStatistics & stats)1677 void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1678   uint32_t ssrc;
1679   {
1680     CriticalSectionScoped ssrc_lock(send_critsect_);
1681     ssrc = ssrc_;
1682   }
1683   if (bitrate_callback_) {
1684     bitrate_callback_->Notify(stats, ssrc);
1685   }
1686 }
1687 
SetRtpState(const RtpState & rtp_state)1688 void RTPSender::SetRtpState(const RtpState& rtp_state) {
1689   SetStartTimestamp(rtp_state.start_timestamp, true);
1690   CriticalSectionScoped lock(send_critsect_);
1691   sequence_number_ = rtp_state.sequence_number;
1692   sequence_number_forced_ = true;
1693   timestamp_ = rtp_state.timestamp;
1694   capture_time_ms_ = rtp_state.capture_time_ms;
1695   last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1696   media_has_been_sent_ = rtp_state.media_has_been_sent;
1697 }
1698 
GetRtpState() const1699 RtpState RTPSender::GetRtpState() const {
1700   CriticalSectionScoped lock(send_critsect_);
1701 
1702   RtpState state;
1703   state.sequence_number = sequence_number_;
1704   state.start_timestamp = start_timestamp_;
1705   state.timestamp = timestamp_;
1706   state.capture_time_ms = capture_time_ms_;
1707   state.last_timestamp_time_ms = last_timestamp_time_ms_;
1708   state.media_has_been_sent = media_has_been_sent_;
1709 
1710   return state;
1711 }
1712 
SetRtxRtpState(const RtpState & rtp_state)1713 void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1714   CriticalSectionScoped lock(send_critsect_);
1715   sequence_number_rtx_ = rtp_state.sequence_number;
1716 }
1717 
GetRtxRtpState() const1718 RtpState RTPSender::GetRtxRtpState() const {
1719   CriticalSectionScoped lock(send_critsect_);
1720 
1721   RtpState state;
1722   state.sequence_number = sequence_number_rtx_;
1723   state.start_timestamp = start_timestamp_;
1724 
1725   return state;
1726 }
1727 
1728 }  // namespace webrtc
1729