• 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 #include <algorithm>
15 #include <utility>
16 
17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/logging.h"
19 #include "webrtc/base/trace_event.h"
20 #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
22 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
23 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
24 #include "webrtc/modules/rtp_rtcp/source/time_util.h"
25 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
26 #include "webrtc/system_wrappers/include/tick_util.h"
27 
28 namespace webrtc {
29 
30 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
31 static const size_t kMaxPaddingLength = 224;
32 static const int kSendSideDelayWindowMs = 1000;
33 static const uint32_t kAbsSendTimeFraction = 18;
34 
35 namespace {
36 
37 const size_t kRtpHeaderLength = 12;
38 const uint16_t kMaxInitRtpSeqNumber = 32767;  // 2^15 -1.
39 
FrameTypeToString(FrameType frame_type)40 const char* FrameTypeToString(FrameType frame_type) {
41   switch (frame_type) {
42     case kEmptyFrame:
43       return "empty";
44     case kAudioFrameSpeech: return "audio_speech";
45     case kAudioFrameCN: return "audio_cn";
46     case kVideoFrameKey: return "video_key";
47     case kVideoFrameDelta: return "video_delta";
48   }
49   return "";
50 }
51 
52 // TODO(holmer): Merge this with the implementation in
53 // remote_bitrate_estimator_abs_send_time.cc.
ConvertMsTo24Bits(int64_t time_ms)54 uint32_t ConvertMsTo24Bits(int64_t time_ms) {
55   uint32_t time_24_bits =
56       static_cast<uint32_t>(
57           ((static_cast<uint64_t>(time_ms) << kAbsSendTimeFraction) + 500) /
58           1000) &
59       0x00FFFFFF;
60   return time_24_bits;
61 }
62 }  // namespace
63 
64 class BitrateAggregator {
65  public:
BitrateAggregator(BitrateStatisticsObserver * bitrate_callback)66   explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
67       : callback_(bitrate_callback),
68         total_bitrate_observer_(*this),
69         retransmit_bitrate_observer_(*this),
70         ssrc_(0) {}
71 
OnStatsUpdated() const72   void OnStatsUpdated() const {
73     if (callback_)
74       callback_->Notify(total_bitrate_observer_.statistics(),
75                         retransmit_bitrate_observer_.statistics(),
76                         ssrc_);
77   }
78 
total_bitrate_observer()79   Bitrate::Observer* total_bitrate_observer() {
80     return &total_bitrate_observer_;
81   }
retransmit_bitrate_observer()82   Bitrate::Observer* retransmit_bitrate_observer() {
83     return &retransmit_bitrate_observer_;
84   }
85 
set_ssrc(uint32_t ssrc)86   void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
87 
88  private:
89   // We assume that these observers are called on the same thread, which is
90   // true for RtpSender as they are called on the Process thread.
91   class BitrateObserver : public Bitrate::Observer {
92    public:
BitrateObserver(const BitrateAggregator & aggregator)93     explicit BitrateObserver(const BitrateAggregator& aggregator)
94         : aggregator_(aggregator) {}
95 
96     // Implements Bitrate::Observer.
BitrateUpdated(const BitrateStatistics & stats)97     void BitrateUpdated(const BitrateStatistics& stats) override {
98       statistics_ = stats;
99       aggregator_.OnStatsUpdated();
100     }
101 
statistics() const102     BitrateStatistics statistics() const { return statistics_; }
103 
104    private:
105     BitrateStatistics statistics_;
106     const BitrateAggregator& aggregator_;
107   };
108 
109   BitrateStatisticsObserver* const callback_;
110   BitrateObserver total_bitrate_observer_;
111   BitrateObserver retransmit_bitrate_observer_;
112   uint32_t ssrc_;
113 };
114 
RTPSender(bool audio,Clock * clock,Transport * transport,RtpAudioFeedback * audio_feedback,RtpPacketSender * paced_sender,TransportSequenceNumberAllocator * sequence_number_allocator,TransportFeedbackObserver * transport_feedback_observer,BitrateStatisticsObserver * bitrate_callback,FrameCountObserver * frame_count_observer,SendSideDelayObserver * send_side_delay_observer)115 RTPSender::RTPSender(
116     bool audio,
117     Clock* clock,
118     Transport* transport,
119     RtpAudioFeedback* audio_feedback,
120     RtpPacketSender* paced_sender,
121     TransportSequenceNumberAllocator* sequence_number_allocator,
122     TransportFeedbackObserver* transport_feedback_observer,
123     BitrateStatisticsObserver* bitrate_callback,
124     FrameCountObserver* frame_count_observer,
125     SendSideDelayObserver* send_side_delay_observer)
126     : clock_(clock),
127       // TODO(holmer): Remove this conversion when we remove the use of
128       // TickTime.
129       clock_delta_ms_(clock_->TimeInMilliseconds() -
130                       TickTime::MillisecondTimestamp()),
131       random_(clock_->TimeInMicroseconds()),
132       bitrates_(new BitrateAggregator(bitrate_callback)),
133       total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
134       audio_configured_(audio),
135       audio_(audio ? new RTPSenderAudio(clock, this, audio_feedback) : nullptr),
136       video_(audio ? nullptr : new RTPSenderVideo(clock, this)),
137       paced_sender_(paced_sender),
138       transport_sequence_number_allocator_(sequence_number_allocator),
139       transport_feedback_observer_(transport_feedback_observer),
140       last_capture_time_ms_sent_(0),
141       send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
142       transport_(transport),
143       sending_media_(true),                      // Default to sending media.
144       max_payload_length_(IP_PACKET_SIZE - 28),  // Default is IP-v4/UDP.
145       packet_over_head_(28),
146       payload_type_(-1),
147       payload_type_map_(),
148       rtp_header_extension_map_(),
149       transmission_time_offset_(0),
150       absolute_send_time_(0),
151       rotation_(kVideoRotation_0),
152       cvo_mode_(kCVONone),
153       transport_sequence_number_(0),
154       // NACK.
155       nack_byte_count_times_(),
156       nack_byte_count_(),
157       nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
158       packet_history_(clock),
159       // Statistics
160       statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
161       rtp_stats_callback_(NULL),
162       frame_count_observer_(frame_count_observer),
163       send_side_delay_observer_(send_side_delay_observer),
164       // RTP variables
165       start_timestamp_forced_(false),
166       start_timestamp_(0),
167       ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
168       remote_ssrc_(0),
169       sequence_number_forced_(false),
170       ssrc_forced_(false),
171       timestamp_(0),
172       capture_time_ms_(0),
173       last_timestamp_time_ms_(0),
174       media_has_been_sent_(false),
175       last_packet_marker_bit_(false),
176       csrcs_(),
177       rtx_(kRtxOff),
178       rtx_payload_type_(-1),
179       target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
180       target_bitrate_(0) {
181   memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
182   memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
183   // We need to seed the random generator.
184   srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
185   ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
186   ssrc_rtx_ = ssrc_db_.CreateSSRC();  // Can't be 0.
187   bitrates_->set_ssrc(ssrc_);
188   // Random start, 16 bits. Can't be 0.
189   sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber);
190   sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
191 }
192 
~RTPSender()193 RTPSender::~RTPSender() {
194   if (remote_ssrc_ != 0) {
195     ssrc_db_.ReturnSSRC(remote_ssrc_);
196   }
197   ssrc_db_.ReturnSSRC(ssrc_);
198 
199   SSRCDatabase::ReturnSSRCDatabase();
200   while (!payload_type_map_.empty()) {
201     std::map<int8_t, RtpUtility::Payload*>::iterator it =
202         payload_type_map_.begin();
203     delete it->second;
204     payload_type_map_.erase(it);
205   }
206 }
207 
SetTargetBitrate(uint32_t bitrate)208 void RTPSender::SetTargetBitrate(uint32_t bitrate) {
209   CriticalSectionScoped cs(target_bitrate_critsect_.get());
210   target_bitrate_ = bitrate;
211 }
212 
GetTargetBitrate()213 uint32_t RTPSender::GetTargetBitrate() {
214   CriticalSectionScoped cs(target_bitrate_critsect_.get());
215   return target_bitrate_;
216 }
217 
ActualSendBitrateKbit() const218 uint16_t RTPSender::ActualSendBitrateKbit() const {
219   return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
220 }
221 
VideoBitrateSent() const222 uint32_t RTPSender::VideoBitrateSent() const {
223   if (video_) {
224     return video_->VideoBitrateSent();
225   }
226   return 0;
227 }
228 
FecOverheadRate() const229 uint32_t RTPSender::FecOverheadRate() const {
230   if (video_) {
231     return video_->FecOverheadRate();
232   }
233   return 0;
234 }
235 
NackOverheadRate() const236 uint32_t RTPSender::NackOverheadRate() const {
237   return nack_bitrate_.BitrateLast();
238 }
239 
SetTransmissionTimeOffset(int32_t transmission_time_offset)240 int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
241   if (transmission_time_offset > (0x800000 - 1) ||
242       transmission_time_offset < -(0x800000 - 1)) {  // Word24.
243     return -1;
244   }
245   CriticalSectionScoped cs(send_critsect_.get());
246   transmission_time_offset_ = transmission_time_offset;
247   return 0;
248 }
249 
SetAbsoluteSendTime(uint32_t absolute_send_time)250 int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
251   if (absolute_send_time > 0xffffff) {  // UWord24.
252     return -1;
253   }
254   CriticalSectionScoped cs(send_critsect_.get());
255   absolute_send_time_ = absolute_send_time;
256   return 0;
257 }
258 
SetVideoRotation(VideoRotation rotation)259 void RTPSender::SetVideoRotation(VideoRotation rotation) {
260   CriticalSectionScoped cs(send_critsect_.get());
261   rotation_ = rotation;
262 }
263 
SetTransportSequenceNumber(uint16_t sequence_number)264 int32_t RTPSender::SetTransportSequenceNumber(uint16_t sequence_number) {
265   CriticalSectionScoped cs(send_critsect_.get());
266   transport_sequence_number_ = sequence_number;
267   return 0;
268 }
269 
RegisterRtpHeaderExtension(RTPExtensionType type,uint8_t id)270 int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
271                                               uint8_t id) {
272   CriticalSectionScoped cs(send_critsect_.get());
273   if (type == kRtpExtensionVideoRotation) {
274     cvo_mode_ = kCVOInactive;
275     return rtp_header_extension_map_.RegisterInactive(type, id);
276   }
277   return rtp_header_extension_map_.Register(type, id);
278 }
279 
IsRtpHeaderExtensionRegistered(RTPExtensionType type)280 bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) {
281   CriticalSectionScoped cs(send_critsect_.get());
282   return rtp_header_extension_map_.IsRegistered(type);
283 }
284 
DeregisterRtpHeaderExtension(RTPExtensionType type)285 int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
286   CriticalSectionScoped cs(send_critsect_.get());
287   return rtp_header_extension_map_.Deregister(type);
288 }
289 
RtpHeaderExtensionTotalLength() const290 size_t RTPSender::RtpHeaderExtensionTotalLength() const {
291   CriticalSectionScoped cs(send_critsect_.get());
292   return rtp_header_extension_map_.GetTotalLengthInBytes();
293 }
294 
RegisterPayload(const char payload_name[RTP_PAYLOAD_NAME_SIZE],int8_t payload_number,uint32_t frequency,size_t channels,uint32_t rate)295 int32_t RTPSender::RegisterPayload(
296     const char payload_name[RTP_PAYLOAD_NAME_SIZE],
297     int8_t payload_number,
298     uint32_t frequency,
299     size_t channels,
300     uint32_t rate) {
301   assert(payload_name);
302   CriticalSectionScoped cs(send_critsect_.get());
303 
304   std::map<int8_t, RtpUtility::Payload*>::iterator it =
305       payload_type_map_.find(payload_number);
306 
307   if (payload_type_map_.end() != it) {
308     // We already use this payload type.
309     RtpUtility::Payload* payload = it->second;
310     assert(payload);
311 
312     // Check if it's the same as we already have.
313     if (RtpUtility::StringCompare(
314             payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
315       if (audio_configured_ && payload->audio &&
316           payload->typeSpecific.Audio.frequency == frequency &&
317           (payload->typeSpecific.Audio.rate == rate ||
318            payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
319         payload->typeSpecific.Audio.rate = rate;
320         // Ensure that we update the rate if new or old is zero.
321         return 0;
322       }
323       if (!audio_configured_ && !payload->audio) {
324         return 0;
325       }
326     }
327     return -1;
328   }
329   int32_t ret_val = 0;
330   RtpUtility::Payload* payload = nullptr;
331   if (audio_configured_) {
332     // TODO(mflodman): Change to CreateAudioPayload and make static.
333     ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
334                                            frequency, channels, rate, &payload);
335   } else {
336     payload = video_->CreateVideoPayload(payload_name, payload_number, rate);
337   }
338   if (payload) {
339     payload_type_map_[payload_number] = payload;
340   }
341   return ret_val;
342 }
343 
DeRegisterSendPayload(int8_t payload_type)344 int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
345   CriticalSectionScoped lock(send_critsect_.get());
346 
347   std::map<int8_t, RtpUtility::Payload*>::iterator it =
348       payload_type_map_.find(payload_type);
349 
350   if (payload_type_map_.end() == it) {
351     return -1;
352   }
353   RtpUtility::Payload* payload = it->second;
354   delete payload;
355   payload_type_map_.erase(it);
356   return 0;
357 }
358 
SetSendPayloadType(int8_t payload_type)359 void RTPSender::SetSendPayloadType(int8_t payload_type) {
360   CriticalSectionScoped cs(send_critsect_.get());
361   payload_type_ = payload_type;
362 }
363 
SendPayloadType() const364 int8_t RTPSender::SendPayloadType() const {
365   CriticalSectionScoped cs(send_critsect_.get());
366   return payload_type_;
367 }
368 
SendPayloadFrequency() const369 int RTPSender::SendPayloadFrequency() const {
370   return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
371 }
372 
SetMaxPayloadLength(size_t max_payload_length,uint16_t packet_over_head)373 int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
374                                        uint16_t packet_over_head) {
375   // Sanity check.
376   RTC_DCHECK(max_payload_length >= 100 && max_payload_length <= IP_PACKET_SIZE)
377       << "Invalid max payload length: " << max_payload_length;
378   CriticalSectionScoped cs(send_critsect_.get());
379   max_payload_length_ = max_payload_length;
380   packet_over_head_ = packet_over_head;
381   return 0;
382 }
383 
MaxDataPayloadLength() const384 size_t RTPSender::MaxDataPayloadLength() const {
385   int rtx;
386   {
387     CriticalSectionScoped rtx_lock(send_critsect_.get());
388     rtx = rtx_;
389   }
390   if (audio_configured_) {
391     return max_payload_length_ - RTPHeaderLength();
392   } else {
393     return max_payload_length_ - RTPHeaderLength()  // RTP overhead.
394            - video_->FECPacketOverhead()            // FEC/ULP/RED overhead.
395            - ((rtx) ? 2 : 0);                       // RTX overhead.
396   }
397 }
398 
MaxPayloadLength() const399 size_t RTPSender::MaxPayloadLength() const {
400   return max_payload_length_;
401 }
402 
PacketOverHead() const403 uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
404 
SetRtxStatus(int mode)405 void RTPSender::SetRtxStatus(int mode) {
406   CriticalSectionScoped cs(send_critsect_.get());
407   rtx_ = mode;
408 }
409 
RtxStatus() const410 int RTPSender::RtxStatus() const {
411   CriticalSectionScoped cs(send_critsect_.get());
412   return rtx_;
413 }
414 
SetRtxSsrc(uint32_t ssrc)415 void RTPSender::SetRtxSsrc(uint32_t ssrc) {
416   CriticalSectionScoped cs(send_critsect_.get());
417   ssrc_rtx_ = ssrc;
418 }
419 
RtxSsrc() const420 uint32_t RTPSender::RtxSsrc() const {
421   CriticalSectionScoped cs(send_critsect_.get());
422   return ssrc_rtx_;
423 }
424 
SetRtxPayloadType(int payload_type,int associated_payload_type)425 void RTPSender::SetRtxPayloadType(int payload_type,
426                                   int associated_payload_type) {
427   CriticalSectionScoped cs(send_critsect_.get());
428   RTC_DCHECK_LE(payload_type, 127);
429   RTC_DCHECK_LE(associated_payload_type, 127);
430   if (payload_type < 0) {
431     LOG(LS_ERROR) << "Invalid RTX payload type: " << payload_type;
432     return;
433   }
434 
435   rtx_payload_type_map_[associated_payload_type] = payload_type;
436   rtx_payload_type_ = payload_type;
437 }
438 
RtxPayloadType() const439 std::pair<int, int> RTPSender::RtxPayloadType() const {
440   CriticalSectionScoped cs(send_critsect_.get());
441   for (const auto& kv : rtx_payload_type_map_) {
442     if (kv.second == rtx_payload_type_) {
443       return std::make_pair(rtx_payload_type_, kv.first);
444     }
445   }
446   return std::make_pair(-1, -1);
447 }
448 
CheckPayloadType(int8_t payload_type,RtpVideoCodecTypes * video_type)449 int32_t RTPSender::CheckPayloadType(int8_t payload_type,
450                                     RtpVideoCodecTypes* video_type) {
451   CriticalSectionScoped cs(send_critsect_.get());
452 
453   if (payload_type < 0) {
454     LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
455     return -1;
456   }
457   if (audio_configured_) {
458     int8_t red_pl_type = -1;
459     if (audio_->RED(&red_pl_type) == 0) {
460       // We have configured RED.
461       if (red_pl_type == payload_type) {
462         // And it's a match...
463         return 0;
464       }
465     }
466   }
467   if (payload_type_ == payload_type) {
468     if (!audio_configured_) {
469       *video_type = video_->VideoCodecType();
470     }
471     return 0;
472   }
473   std::map<int8_t, RtpUtility::Payload*>::iterator it =
474       payload_type_map_.find(payload_type);
475   if (it == payload_type_map_.end()) {
476     LOG(LS_WARNING) << "Payload type " << static_cast<int>(payload_type)
477                     << " not registered.";
478     return -1;
479   }
480   SetSendPayloadType(payload_type);
481   RtpUtility::Payload* payload = it->second;
482   assert(payload);
483   if (!payload->audio && !audio_configured_) {
484     video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
485     *video_type = payload->typeSpecific.Video.videoCodecType;
486     video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
487   }
488   return 0;
489 }
490 
ActivateCVORtpHeaderExtension()491 RTPSenderInterface::CVOMode RTPSender::ActivateCVORtpHeaderExtension() {
492   if (cvo_mode_ == kCVOInactive) {
493     CriticalSectionScoped cs(send_critsect_.get());
494     if (rtp_header_extension_map_.SetActive(kRtpExtensionVideoRotation, true)) {
495       cvo_mode_ = kCVOActivated;
496     }
497   }
498   return cvo_mode_;
499 }
500 
SendOutgoingData(FrameType frame_type,int8_t payload_type,uint32_t capture_timestamp,int64_t capture_time_ms,const uint8_t * payload_data,size_t payload_size,const RTPFragmentationHeader * fragmentation,const RTPVideoHeader * rtp_hdr)501 int32_t RTPSender::SendOutgoingData(FrameType frame_type,
502                                     int8_t payload_type,
503                                     uint32_t capture_timestamp,
504                                     int64_t capture_time_ms,
505                                     const uint8_t* payload_data,
506                                     size_t payload_size,
507                                     const RTPFragmentationHeader* fragmentation,
508                                     const RTPVideoHeader* rtp_hdr) {
509   uint32_t ssrc;
510   {
511     // Drop this packet if we're not sending media packets.
512     CriticalSectionScoped cs(send_critsect_.get());
513     ssrc = ssrc_;
514     if (!sending_media_) {
515       return 0;
516     }
517   }
518   RtpVideoCodecTypes video_type = kRtpVideoGeneric;
519   if (CheckPayloadType(payload_type, &video_type) != 0) {
520     LOG(LS_ERROR) << "Don't send data with unknown payload type: "
521                   << static_cast<int>(payload_type) << ".";
522     return -1;
523   }
524 
525   int32_t ret_val;
526   if (audio_configured_) {
527     TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
528                             "Send", "type", FrameTypeToString(frame_type));
529     assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
530            frame_type == kEmptyFrame);
531 
532     ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
533                                 payload_data, payload_size, fragmentation);
534   } else {
535     TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
536                             "Send", "type", FrameTypeToString(frame_type));
537     assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
538 
539     if (frame_type == kEmptyFrame)
540       return 0;
541 
542     ret_val =
543         video_->SendVideo(video_type, frame_type, payload_type,
544                           capture_timestamp, capture_time_ms, payload_data,
545                           payload_size, fragmentation, rtp_hdr);
546   }
547 
548   CriticalSectionScoped cs(statistics_crit_.get());
549   // Note: This is currently only counting for video.
550   if (frame_type == kVideoFrameKey) {
551     ++frame_counts_.key_frames;
552   } else if (frame_type == kVideoFrameDelta) {
553     ++frame_counts_.delta_frames;
554   }
555   if (frame_count_observer_) {
556     frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
557   }
558 
559   return ret_val;
560 }
561 
TrySendRedundantPayloads(size_t bytes_to_send)562 size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
563   {
564     CriticalSectionScoped cs(send_critsect_.get());
565     if ((rtx_ & kRtxRedundantPayloads) == 0)
566       return 0;
567   }
568 
569   uint8_t buffer[IP_PACKET_SIZE];
570   int bytes_left = static_cast<int>(bytes_to_send);
571   while (bytes_left > 0) {
572     size_t length = bytes_left;
573     int64_t capture_time_ms;
574     if (!packet_history_.GetBestFittingPacket(buffer, &length,
575                                               &capture_time_ms)) {
576       break;
577     }
578     if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
579       break;
580     RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
581     RTPHeader rtp_header;
582     rtp_parser.Parse(&rtp_header);
583     bytes_left -= static_cast<int>(length - rtp_header.headerLength);
584   }
585   return bytes_to_send - bytes_left;
586 }
587 
BuildPaddingPacket(uint8_t * packet,size_t header_length,size_t padding_length)588 void RTPSender::BuildPaddingPacket(uint8_t* packet,
589                                    size_t header_length,
590                                    size_t padding_length) {
591   packet[0] |= 0x20;  // Set padding bit.
592   int32_t* data = reinterpret_cast<int32_t*>(&(packet[header_length]));
593 
594   // Fill data buffer with random data.
595   for (size_t j = 0; j < (padding_length >> 2); ++j) {
596     data[j] = rand();  // NOLINT
597   }
598   // Set number of padding bytes in the last byte of the packet.
599   packet[header_length + padding_length - 1] =
600       static_cast<uint8_t>(padding_length);
601 }
602 
SendPadData(size_t bytes,bool timestamp_provided,uint32_t timestamp,int64_t capture_time_ms)603 size_t RTPSender::SendPadData(size_t bytes,
604                               bool timestamp_provided,
605                               uint32_t timestamp,
606                               int64_t capture_time_ms) {
607   // Always send full padding packets. This is accounted for by the
608   // RtpPacketSender,
609   // which will make sure we don't send too much padding even if a single packet
610   // is larger than requested.
611   size_t padding_bytes_in_packet =
612       std::min(MaxDataPayloadLength(), kMaxPaddingLength);
613   size_t bytes_sent = 0;
614   bool using_transport_seq = rtp_header_extension_map_.IsRegistered(
615                                  kRtpExtensionTransportSequenceNumber) &&
616                              transport_sequence_number_allocator_;
617   for (; bytes > 0; bytes -= padding_bytes_in_packet) {
618     if (bytes < padding_bytes_in_packet)
619       bytes = padding_bytes_in_packet;
620 
621     uint32_t ssrc;
622     uint16_t sequence_number;
623     int payload_type;
624     bool over_rtx;
625     {
626       CriticalSectionScoped cs(send_critsect_.get());
627       if (!timestamp_provided) {
628         timestamp = timestamp_;
629         capture_time_ms = capture_time_ms_;
630       }
631       if (rtx_ == kRtxOff) {
632         // Without RTX we can't send padding in the middle of frames.
633         if (!last_packet_marker_bit_)
634           return 0;
635         ssrc = ssrc_;
636         sequence_number = sequence_number_;
637         ++sequence_number_;
638         payload_type = payload_type_;
639         over_rtx = false;
640       } else {
641         // Without abs-send-time a media packet must be sent before padding so
642         // that the timestamps used for estimation are correct.
643         if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
644             kRtpExtensionAbsoluteSendTime))
645           return 0;
646         // Only change change the timestamp of padding packets sent over RTX.
647         // Padding only packets over RTP has to be sent as part of a media
648         // frame (and therefore the same timestamp).
649         if (last_timestamp_time_ms_ > 0) {
650           timestamp +=
651               (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
652           capture_time_ms +=
653               (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
654         }
655         ssrc = ssrc_rtx_;
656         sequence_number = sequence_number_rtx_;
657         ++sequence_number_rtx_;
658         payload_type = rtx_payload_type_;
659         over_rtx = true;
660       }
661     }
662 
663     uint8_t padding_packet[IP_PACKET_SIZE];
664     size_t header_length =
665         CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
666                         sequence_number, std::vector<uint32_t>());
667     BuildPaddingPacket(padding_packet, header_length, padding_bytes_in_packet);
668     size_t length = padding_bytes_in_packet + header_length;
669     int64_t now_ms = clock_->TimeInMilliseconds();
670 
671     RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
672     RTPHeader rtp_header;
673     rtp_parser.Parse(&rtp_header);
674 
675     if (capture_time_ms > 0) {
676       UpdateTransmissionTimeOffset(
677           padding_packet, length, rtp_header, now_ms - capture_time_ms);
678     }
679 
680     UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
681 
682     PacketOptions options;
683     if (using_transport_seq) {
684       options.packet_id =
685           UpdateTransportSequenceNumber(padding_packet, length, rtp_header);
686     }
687 
688     if (using_transport_seq && transport_feedback_observer_) {
689       transport_feedback_observer_->AddPacket(options.packet_id, length, true);
690     }
691 
692     if (!SendPacketToNetwork(padding_packet, length, options))
693       break;
694 
695     bytes_sent += padding_bytes_in_packet;
696     UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
697   }
698 
699   return bytes_sent;
700 }
701 
SetStorePacketsStatus(bool enable,uint16_t number_to_store)702 void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
703   packet_history_.SetStorePacketsStatus(enable, number_to_store);
704 }
705 
StorePackets() const706 bool RTPSender::StorePackets() const {
707   return packet_history_.StorePackets();
708 }
709 
ReSendPacket(uint16_t packet_id,int64_t min_resend_time)710 int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
711   size_t length = IP_PACKET_SIZE;
712   uint8_t data_buffer[IP_PACKET_SIZE];
713   int64_t capture_time_ms;
714 
715   if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
716                                                data_buffer, &length,
717                                                &capture_time_ms)) {
718     // Packet not found.
719     return 0;
720   }
721 
722   if (paced_sender_) {
723     RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
724     RTPHeader header;
725     if (!rtp_parser.Parse(&header)) {
726       assert(false);
727       return -1;
728     }
729     // Convert from TickTime to Clock since capture_time_ms is based on
730     // TickTime.
731     int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
732     paced_sender_->InsertPacket(
733         RtpPacketSender::kNormalPriority, header.ssrc, header.sequenceNumber,
734         corrected_capture_tims_ms, length - header.headerLength, true);
735 
736     return length;
737   }
738   int rtx = kRtxOff;
739   {
740     CriticalSectionScoped lock(send_critsect_.get());
741     rtx = rtx_;
742   }
743   if (!PrepareAndSendPacket(data_buffer, length, capture_time_ms,
744                             (rtx & kRtxRetransmitted) > 0, true)) {
745     return -1;
746   }
747   return static_cast<int32_t>(length);
748 }
749 
SendPacketToNetwork(const uint8_t * packet,size_t size,const PacketOptions & options)750 bool RTPSender::SendPacketToNetwork(const uint8_t* packet,
751                                     size_t size,
752                                     const PacketOptions& options) {
753   int bytes_sent = -1;
754   if (transport_) {
755     bytes_sent = transport_->SendRtp(packet, size, options)
756                      ? static_cast<int>(size)
757                      : -1;
758   }
759   TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
760                        "RTPSender::SendPacketToNetwork", "size", size, "sent",
761                        bytes_sent);
762   // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
763   if (bytes_sent <= 0) {
764     LOG(LS_WARNING) << "Transport failed to send packet";
765     return false;
766   }
767   return true;
768 }
769 
SelectiveRetransmissions() const770 int RTPSender::SelectiveRetransmissions() const {
771   if (!video_)
772     return -1;
773   return video_->SelectiveRetransmissions();
774 }
775 
SetSelectiveRetransmissions(uint8_t settings)776 int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
777   if (!video_)
778     return -1;
779   video_->SetSelectiveRetransmissions(settings);
780   return 0;
781 }
782 
OnReceivedNACK(const std::list<uint16_t> & nack_sequence_numbers,int64_t avg_rtt)783 void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
784                                int64_t avg_rtt) {
785   TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
786                "RTPSender::OnReceivedNACK", "num_seqnum",
787                nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
788   const int64_t now = clock_->TimeInMilliseconds();
789   uint32_t bytes_re_sent = 0;
790   uint32_t target_bitrate = GetTargetBitrate();
791 
792   // Enough bandwidth to send NACK?
793   if (!ProcessNACKBitRate(now)) {
794     LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
795                  << target_bitrate;
796     return;
797   }
798 
799   for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
800       it != nack_sequence_numbers.end(); ++it) {
801     const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
802     if (bytes_sent > 0) {
803       bytes_re_sent += bytes_sent;
804     } else if (bytes_sent == 0) {
805       // The packet has previously been resent.
806       // Try resending next packet in the list.
807       continue;
808     } else {
809       // Failed to send one Sequence number. Give up the rest in this nack.
810       LOG(LS_WARNING) << "Failed resending RTP packet " << *it
811                       << ", Discard rest of packets";
812       break;
813     }
814     // Delay bandwidth estimate (RTT * BW).
815     if (target_bitrate != 0 && avg_rtt) {
816       // kbits/s * ms = bits => bits/8 = bytes
817       size_t target_bytes =
818           (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
819       if (bytes_re_sent > target_bytes) {
820         break;  // Ignore the rest of the packets in the list.
821       }
822     }
823   }
824   if (bytes_re_sent > 0) {
825     UpdateNACKBitRate(bytes_re_sent, now);
826   }
827 }
828 
ProcessNACKBitRate(uint32_t now)829 bool RTPSender::ProcessNACKBitRate(uint32_t now) {
830   uint32_t num = 0;
831   size_t byte_count = 0;
832   const uint32_t kAvgIntervalMs = 1000;
833   uint32_t target_bitrate = GetTargetBitrate();
834 
835   CriticalSectionScoped cs(send_critsect_.get());
836 
837   if (target_bitrate == 0) {
838     return true;
839   }
840   for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
841     if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
842       // Don't use data older than 1sec.
843       break;
844     } else {
845       byte_count += nack_byte_count_[num];
846     }
847   }
848   uint32_t time_interval = kAvgIntervalMs;
849   if (num == NACK_BYTECOUNT_SIZE) {
850     // More than NACK_BYTECOUNT_SIZE nack messages has been received
851     // during the last msg_interval.
852     if (nack_byte_count_times_[num - 1] <= now) {
853       time_interval = now - nack_byte_count_times_[num - 1];
854     }
855   }
856   return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
857 }
858 
UpdateNACKBitRate(uint32_t bytes,int64_t now)859 void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
860   CriticalSectionScoped cs(send_critsect_.get());
861   if (bytes == 0)
862     return;
863   nack_bitrate_.Update(bytes);
864   // Save bitrate statistics.
865   // Shift all but first time.
866   for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
867     nack_byte_count_[i + 1] = nack_byte_count_[i];
868     nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
869   }
870   nack_byte_count_[0] = bytes;
871   nack_byte_count_times_[0] = now;
872 }
873 
874 // Called from pacer when we can send the packet.
TimeToSendPacket(uint16_t sequence_number,int64_t capture_time_ms,bool retransmission)875 bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
876                                  int64_t capture_time_ms,
877                                  bool retransmission) {
878   size_t length = IP_PACKET_SIZE;
879   uint8_t data_buffer[IP_PACKET_SIZE];
880   int64_t stored_time_ms;
881 
882   if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
883                                                0,
884                                                retransmission,
885                                                data_buffer,
886                                                &length,
887                                                &stored_time_ms)) {
888     // Packet cannot be found. Allow sending to continue.
889     return true;
890   }
891   if (!retransmission && capture_time_ms > 0) {
892     UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
893   }
894   int rtx;
895   {
896     CriticalSectionScoped lock(send_critsect_.get());
897     rtx = rtx_;
898   }
899   return PrepareAndSendPacket(data_buffer,
900                               length,
901                               capture_time_ms,
902                               retransmission && (rtx & kRtxRetransmitted) > 0,
903                               retransmission);
904 }
905 
PrepareAndSendPacket(uint8_t * buffer,size_t length,int64_t capture_time_ms,bool send_over_rtx,bool is_retransmit)906 bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
907                                      size_t length,
908                                      int64_t capture_time_ms,
909                                      bool send_over_rtx,
910                                      bool is_retransmit) {
911   uint8_t* buffer_to_send_ptr = buffer;
912 
913   RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
914   RTPHeader rtp_header;
915   rtp_parser.Parse(&rtp_header);
916   if (!is_retransmit && rtp_header.markerBit) {
917     TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
918                            capture_time_ms);
919   }
920 
921   TRACE_EVENT_INSTANT2(
922       TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
923       "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
924 
925   uint8_t data_buffer_rtx[IP_PACKET_SIZE];
926   if (send_over_rtx) {
927     BuildRtxPacket(buffer, &length, data_buffer_rtx);
928     buffer_to_send_ptr = data_buffer_rtx;
929   }
930 
931   int64_t now_ms = clock_->TimeInMilliseconds();
932   int64_t diff_ms = now_ms - capture_time_ms;
933   UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
934                                diff_ms);
935   UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
936 
937   // TODO(sprang): Potentially too much overhead in IsRegistered()?
938   bool using_transport_seq = rtp_header_extension_map_.IsRegistered(
939                                  kRtpExtensionTransportSequenceNumber) &&
940                              transport_sequence_number_allocator_;
941 
942   PacketOptions options;
943   if (using_transport_seq) {
944     options.packet_id =
945         UpdateTransportSequenceNumber(buffer_to_send_ptr, length, rtp_header);
946   }
947 
948   if (using_transport_seq && transport_feedback_observer_) {
949     transport_feedback_observer_->AddPacket(options.packet_id, length, true);
950   }
951 
952   bool ret = SendPacketToNetwork(buffer_to_send_ptr, length, options);
953   if (ret) {
954     CriticalSectionScoped lock(send_critsect_.get());
955     media_has_been_sent_ = true;
956   }
957   UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
958                  is_retransmit);
959   return ret;
960 }
961 
UpdateRtpStats(const uint8_t * buffer,size_t packet_length,const RTPHeader & header,bool is_rtx,bool is_retransmit)962 void RTPSender::UpdateRtpStats(const uint8_t* buffer,
963                                size_t packet_length,
964                                const RTPHeader& header,
965                                bool is_rtx,
966                                bool is_retransmit) {
967   StreamDataCounters* counters;
968   // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
969   uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
970 
971   CriticalSectionScoped lock(statistics_crit_.get());
972   if (is_rtx) {
973     counters = &rtx_rtp_stats_;
974   } else {
975     counters = &rtp_stats_;
976   }
977 
978   total_bitrate_sent_.Update(packet_length);
979 
980   if (counters->first_packet_time_ms == -1) {
981     counters->first_packet_time_ms = clock_->TimeInMilliseconds();
982   }
983   if (IsFecPacket(buffer, header)) {
984     counters->fec.AddPacket(packet_length, header);
985   }
986   if (is_retransmit) {
987     counters->retransmitted.AddPacket(packet_length, header);
988   }
989   counters->transmitted.AddPacket(packet_length, header);
990 
991   if (rtp_stats_callback_) {
992     rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
993   }
994 }
995 
IsFecPacket(const uint8_t * buffer,const RTPHeader & header) const996 bool RTPSender::IsFecPacket(const uint8_t* buffer,
997                             const RTPHeader& header) const {
998   if (!video_) {
999     return false;
1000   }
1001   bool fec_enabled;
1002   uint8_t pt_red;
1003   uint8_t pt_fec;
1004   video_->GenericFECStatus(&fec_enabled, &pt_red, &pt_fec);
1005   return fec_enabled &&
1006       header.payloadType == pt_red &&
1007       buffer[header.headerLength] == pt_fec;
1008 }
1009 
TimeToSendPadding(size_t bytes)1010 size_t RTPSender::TimeToSendPadding(size_t bytes) {
1011   if (audio_configured_ || bytes == 0)
1012     return 0;
1013   {
1014     CriticalSectionScoped cs(send_critsect_.get());
1015     if (!sending_media_)
1016       return 0;
1017   }
1018   size_t bytes_sent = TrySendRedundantPayloads(bytes);
1019   if (bytes_sent < bytes)
1020     bytes_sent += SendPadData(bytes - bytes_sent, false, 0, 0);
1021   return bytes_sent;
1022 }
1023 
1024 // TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
SendToNetwork(uint8_t * buffer,size_t payload_length,size_t rtp_header_length,int64_t capture_time_ms,StorageType storage,RtpPacketSender::Priority priority)1025 int32_t RTPSender::SendToNetwork(uint8_t* buffer,
1026                                  size_t payload_length,
1027                                  size_t rtp_header_length,
1028                                  int64_t capture_time_ms,
1029                                  StorageType storage,
1030                                  RtpPacketSender::Priority priority) {
1031   RtpUtility::RtpHeaderParser rtp_parser(buffer,
1032                                          payload_length + rtp_header_length);
1033   RTPHeader rtp_header;
1034   rtp_parser.Parse(&rtp_header);
1035 
1036   int64_t now_ms = clock_->TimeInMilliseconds();
1037 
1038   // |capture_time_ms| <= 0 is considered invalid.
1039   // TODO(holmer): This should be changed all over Video Engine so that negative
1040   // time is consider invalid, while 0 is considered a valid time.
1041   if (capture_time_ms > 0) {
1042     UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
1043                                  rtp_header, now_ms - capture_time_ms);
1044   }
1045 
1046   UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
1047                          rtp_header, now_ms);
1048 
1049   // Used for NACK and to spread out the transmission of packets.
1050   if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
1051                                    capture_time_ms, storage) != 0) {
1052     return -1;
1053   }
1054 
1055   if (paced_sender_) {
1056     // Correct offset between implementations of millisecond time stamps in
1057     // TickTime and Clock.
1058     int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
1059     paced_sender_->InsertPacket(priority, rtp_header.ssrc,
1060                                 rtp_header.sequenceNumber, corrected_time_ms,
1061                                 payload_length, false);
1062     if (last_capture_time_ms_sent_ == 0 ||
1063         corrected_time_ms > last_capture_time_ms_sent_) {
1064       last_capture_time_ms_sent_ = corrected_time_ms;
1065       TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
1066                                "PacedSend", corrected_time_ms,
1067                                "capture_time_ms", corrected_time_ms);
1068     }
1069     return 0;
1070   }
1071   if (capture_time_ms > 0) {
1072     UpdateDelayStatistics(capture_time_ms, now_ms);
1073   }
1074 
1075   size_t length = payload_length + rtp_header_length;
1076   bool sent = SendPacketToNetwork(buffer, length, PacketOptions());
1077 
1078   // Mark the packet as sent in the history even if send failed. Dropping a
1079   // packet here should be treated as any other packet drop so we should be
1080   // ready for a retransmission.
1081   packet_history_.SetSent(rtp_header.sequenceNumber);
1082 
1083   if (!sent)
1084     return -1;
1085 
1086   {
1087     CriticalSectionScoped lock(send_critsect_.get());
1088     media_has_been_sent_ = true;
1089   }
1090   UpdateRtpStats(buffer, length, rtp_header, false, false);
1091   return 0;
1092 }
1093 
UpdateDelayStatistics(int64_t capture_time_ms,int64_t now_ms)1094 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
1095   if (!send_side_delay_observer_)
1096     return;
1097 
1098   uint32_t ssrc;
1099   int avg_delay_ms = 0;
1100   int max_delay_ms = 0;
1101   {
1102     CriticalSectionScoped lock(send_critsect_.get());
1103     ssrc = ssrc_;
1104   }
1105   {
1106     CriticalSectionScoped cs(statistics_crit_.get());
1107     // TODO(holmer): Compute this iteratively instead.
1108     send_delays_[now_ms] = now_ms - capture_time_ms;
1109     send_delays_.erase(send_delays_.begin(),
1110                        send_delays_.lower_bound(now_ms -
1111                        kSendSideDelayWindowMs));
1112     int num_delays = 0;
1113     for (auto it = send_delays_.upper_bound(now_ms - kSendSideDelayWindowMs);
1114          it != send_delays_.end(); ++it) {
1115       max_delay_ms = std::max(max_delay_ms, it->second);
1116       avg_delay_ms += it->second;
1117       ++num_delays;
1118     }
1119     if (num_delays == 0)
1120       return;
1121     avg_delay_ms = (avg_delay_ms + num_delays / 2) / num_delays;
1122   }
1123   send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms, max_delay_ms,
1124                                                   ssrc);
1125 }
1126 
ProcessBitrate()1127 void RTPSender::ProcessBitrate() {
1128   CriticalSectionScoped cs(send_critsect_.get());
1129   total_bitrate_sent_.Process();
1130   nack_bitrate_.Process();
1131   if (audio_configured_) {
1132     return;
1133   }
1134   video_->ProcessBitrate();
1135 }
1136 
RTPHeaderLength() const1137 size_t RTPSender::RTPHeaderLength() const {
1138   CriticalSectionScoped lock(send_critsect_.get());
1139   size_t rtp_header_length = kRtpHeaderLength;
1140   rtp_header_length += sizeof(uint32_t) * csrcs_.size();
1141   rtp_header_length += RtpHeaderExtensionTotalLength();
1142   return rtp_header_length;
1143 }
1144 
AllocateSequenceNumber(uint16_t packets_to_send)1145 uint16_t RTPSender::AllocateSequenceNumber(uint16_t packets_to_send) {
1146   CriticalSectionScoped cs(send_critsect_.get());
1147   uint16_t first_allocated_sequence_number = sequence_number_;
1148   sequence_number_ += packets_to_send;
1149   return first_allocated_sequence_number;
1150 }
1151 
GetDataCounters(StreamDataCounters * rtp_stats,StreamDataCounters * rtx_stats) const1152 void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1153                                 StreamDataCounters* rtx_stats) const {
1154   CriticalSectionScoped lock(statistics_crit_.get());
1155   *rtp_stats = rtp_stats_;
1156   *rtx_stats = rtx_rtp_stats_;
1157 }
1158 
CreateRtpHeader(uint8_t * header,int8_t payload_type,uint32_t ssrc,bool marker_bit,uint32_t timestamp,uint16_t sequence_number,const std::vector<uint32_t> & csrcs) const1159 size_t RTPSender::CreateRtpHeader(uint8_t* header,
1160                                   int8_t payload_type,
1161                                   uint32_t ssrc,
1162                                   bool marker_bit,
1163                                   uint32_t timestamp,
1164                                   uint16_t sequence_number,
1165                                   const std::vector<uint32_t>& csrcs) const {
1166   header[0] = 0x80;  // version 2.
1167   header[1] = static_cast<uint8_t>(payload_type);
1168   if (marker_bit) {
1169     header[1] |= kRtpMarkerBitMask;  // Marker bit is set.
1170   }
1171   ByteWriter<uint16_t>::WriteBigEndian(header + 2, sequence_number);
1172   ByteWriter<uint32_t>::WriteBigEndian(header + 4, timestamp);
1173   ByteWriter<uint32_t>::WriteBigEndian(header + 8, ssrc);
1174   int32_t rtp_header_length = kRtpHeaderLength;
1175 
1176   if (csrcs.size() > 0) {
1177     uint8_t* ptr = &header[rtp_header_length];
1178     for (size_t i = 0; i < csrcs.size(); ++i) {
1179       ByteWriter<uint32_t>::WriteBigEndian(ptr, csrcs[i]);
1180       ptr += 4;
1181     }
1182     header[0] = (header[0] & 0xf0) | csrcs.size();
1183 
1184     // Update length of header.
1185     rtp_header_length += sizeof(uint32_t) * csrcs.size();
1186   }
1187 
1188   uint16_t len =
1189       BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
1190   if (len > 0) {
1191     header[0] |= 0x10;  // Set extension bit.
1192     rtp_header_length += len;
1193   }
1194   return rtp_header_length;
1195 }
1196 
BuildRTPheader(uint8_t * data_buffer,int8_t payload_type,bool marker_bit,uint32_t capture_timestamp,int64_t capture_time_ms,bool timestamp_provided,bool inc_sequence_number)1197 int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1198                                   int8_t payload_type,
1199                                   bool marker_bit,
1200                                   uint32_t capture_timestamp,
1201                                   int64_t capture_time_ms,
1202                                   bool timestamp_provided,
1203                                   bool inc_sequence_number) {
1204   assert(payload_type >= 0);
1205   CriticalSectionScoped cs(send_critsect_.get());
1206 
1207   if (timestamp_provided) {
1208     timestamp_ = start_timestamp_ + capture_timestamp;
1209   } else {
1210     // Make a unique time stamp.
1211     // We can't inc by the actual time, since then we increase the risk of back
1212     // timing.
1213     timestamp_++;
1214   }
1215   last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
1216   uint32_t sequence_number = sequence_number_++;
1217   capture_time_ms_ = capture_time_ms;
1218   last_packet_marker_bit_ = marker_bit;
1219   return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1220                          timestamp_, sequence_number, csrcs_);
1221 }
1222 
BuildRTPHeaderExtension(uint8_t * data_buffer,bool marker_bit) const1223 uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1224                                             bool marker_bit) const {
1225   if (rtp_header_extension_map_.Size() <= 0) {
1226     return 0;
1227   }
1228   // RTP header extension, RFC 3550.
1229   //   0                   1                   2                   3
1230   //   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
1231   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1232   //  |      defined by profile       |           length              |
1233   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1234   //  |                        header extension                       |
1235   //  |                             ....                              |
1236   //
1237   const uint32_t kPosLength = 2;
1238   const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
1239 
1240   // Add extension ID (0xBEDE).
1241   ByteWriter<uint16_t>::WriteBigEndian(data_buffer,
1242                                        kRtpOneByteHeaderExtensionId);
1243 
1244   // Add extensions.
1245   uint16_t total_block_length = 0;
1246 
1247   RTPExtensionType type = rtp_header_extension_map_.First();
1248   while (type != kRtpExtensionNone) {
1249     uint8_t block_length = 0;
1250     uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
1251     switch (type) {
1252       case kRtpExtensionTransmissionTimeOffset:
1253         block_length = BuildTransmissionTimeOffsetExtension(extension_data);
1254         break;
1255       case kRtpExtensionAudioLevel:
1256         block_length = BuildAudioLevelExtension(extension_data);
1257         break;
1258       case kRtpExtensionAbsoluteSendTime:
1259         block_length = BuildAbsoluteSendTimeExtension(extension_data);
1260         break;
1261       case kRtpExtensionVideoRotation:
1262         block_length = BuildVideoRotationExtension(extension_data);
1263         break;
1264       case kRtpExtensionTransportSequenceNumber:
1265         block_length = BuildTransportSequenceNumberExtension(
1266             extension_data, transport_sequence_number_);
1267         break;
1268       default:
1269         assert(false);
1270     }
1271     total_block_length += block_length;
1272     type = rtp_header_extension_map_.Next(type);
1273   }
1274   if (total_block_length == 0) {
1275     // No extension added.
1276     return 0;
1277   }
1278   // Add padding elements until we've filled a 32 bit block.
1279   size_t padding_bytes =
1280       RtpUtility::Word32Align(total_block_length) - total_block_length;
1281   if (padding_bytes > 0) {
1282     memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1283     total_block_length += padding_bytes;
1284   }
1285   // Set header length (in number of Word32, header excluded).
1286   ByteWriter<uint16_t>::WriteBigEndian(data_buffer + kPosLength,
1287                                        total_block_length / 4);
1288   // Total added length.
1289   return kHeaderLength + total_block_length;
1290 }
1291 
BuildTransmissionTimeOffsetExtension(uint8_t * data_buffer) const1292 uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1293     uint8_t* data_buffer) const {
1294   // From RFC 5450: Transmission Time Offsets in RTP Streams.
1295   //
1296   // The transmission time is signaled to the receiver in-band using the
1297   // general mechanism for RTP header extensions [RFC5285]. The payload
1298   // of this extension (the transmitted value) is a 24-bit signed integer.
1299   // When added to the RTP timestamp of the packet, it represents the
1300   // "effective" RTP transmission time of the packet, on the RTP
1301   // timescale.
1302   //
1303   // The form of the transmission offset extension block:
1304   //
1305   //    0                   1                   2                   3
1306   //    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
1307   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1308   //   |  ID   | len=2 |              transmission offset              |
1309   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1310 
1311   // Get id defined by user.
1312   uint8_t id;
1313   if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1314                                       &id) != 0) {
1315     // Not registered.
1316     return 0;
1317   }
1318   size_t pos = 0;
1319   const uint8_t len = 2;
1320   data_buffer[pos++] = (id << 4) + len;
1321   ByteWriter<int32_t, 3>::WriteBigEndian(data_buffer + pos,
1322                                          transmission_time_offset_);
1323   pos += 3;
1324   assert(pos == kTransmissionTimeOffsetLength);
1325   return kTransmissionTimeOffsetLength;
1326 }
1327 
BuildAudioLevelExtension(uint8_t * data_buffer) const1328 uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1329   // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1330   //
1331   // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1332   //
1333   // The form of the audio level extension block:
1334   //
1335   //    0                   1
1336   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1337   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1338   //   |  ID   | len=0 |V|   level     |
1339   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1340   //
1341 
1342   // Get id defined by user.
1343   uint8_t id;
1344   if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1345     // Not registered.
1346     return 0;
1347   }
1348   size_t pos = 0;
1349   const uint8_t len = 0;
1350   data_buffer[pos++] = (id << 4) + len;
1351   data_buffer[pos++] = (1 << 7) + 0;     // Voice, 0 dBov.
1352   assert(pos == kAudioLevelLength);
1353   return kAudioLevelLength;
1354 }
1355 
BuildAbsoluteSendTimeExtension(uint8_t * data_buffer) const1356 uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
1357   // Absolute send time in RTP streams.
1358   //
1359   // The absolute send time is signaled to the receiver in-band using the
1360   // general mechanism for RTP header extensions [RFC5285]. The payload
1361   // of this extension (the transmitted value) is a 24-bit unsigned integer
1362   // containing the sender's current time in seconds as a fixed point number
1363   // with 18 bits fractional part.
1364   //
1365   // The form of the absolute send time extension block:
1366   //
1367   //    0                   1                   2                   3
1368   //    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
1369   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1370   //   |  ID   | len=2 |              absolute send time               |
1371   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1372 
1373   // Get id defined by user.
1374   uint8_t id;
1375   if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1376                                       &id) != 0) {
1377     // Not registered.
1378     return 0;
1379   }
1380   size_t pos = 0;
1381   const uint8_t len = 2;
1382   data_buffer[pos++] = (id << 4) + len;
1383   ByteWriter<uint32_t, 3>::WriteBigEndian(data_buffer + pos,
1384                                           absolute_send_time_);
1385   pos += 3;
1386   assert(pos == kAbsoluteSendTimeLength);
1387   return kAbsoluteSendTimeLength;
1388 }
1389 
BuildVideoRotationExtension(uint8_t * data_buffer) const1390 uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1391   // Coordination of Video Orientation in RTP streams.
1392   //
1393   // Coordination of Video Orientation consists in signaling of the current
1394   // orientation of the image captured on the sender side to the receiver for
1395   // appropriate rendering and displaying.
1396   //
1397   //    0                   1
1398   //    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1399   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1400   //   |  ID   | len=0 |0 0 0 0 C F R R|
1401   //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1402   //
1403 
1404   // Get id defined by user.
1405   uint8_t id;
1406   if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1407     // Not registered.
1408     return 0;
1409   }
1410   size_t pos = 0;
1411   const uint8_t len = 0;
1412   data_buffer[pos++] = (id << 4) + len;
1413   data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
1414   assert(pos == kVideoRotationLength);
1415   return kVideoRotationLength;
1416 }
1417 
BuildTransportSequenceNumberExtension(uint8_t * data_buffer,uint16_t sequence_number) const1418 uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1419     uint8_t* data_buffer,
1420     uint16_t sequence_number) const {
1421   //   0                   1                   2
1422   //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1423   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1424   //  |  ID   | L=1   |transport wide sequence number |
1425   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1426 
1427   // Get id defined by user.
1428   uint8_t id;
1429   if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1430                                       &id) != 0) {
1431     // Not registered.
1432     return 0;
1433   }
1434   size_t pos = 0;
1435   const uint8_t len = 1;
1436   data_buffer[pos++] = (id << 4) + len;
1437   ByteWriter<uint16_t>::WriteBigEndian(data_buffer + pos, sequence_number);
1438   pos += 2;
1439   assert(pos == kTransportSequenceNumberLength);
1440   return kTransportSequenceNumberLength;
1441 }
1442 
FindHeaderExtensionPosition(RTPExtensionType type,const uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,size_t * position) const1443 bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1444                                             const uint8_t* rtp_packet,
1445                                             size_t rtp_packet_length,
1446                                             const RTPHeader& rtp_header,
1447                                             size_t* position) const {
1448   // Get length until start of header extension block.
1449   int extension_block_pos =
1450       rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1451   if (extension_block_pos < 0) {
1452     LOG(LS_WARNING) << "Failed to find extension position for " << type
1453                     << " as it is not registered.";
1454     return false;
1455   }
1456 
1457   HeaderExtension header_extension(type);
1458 
1459   size_t block_pos =
1460       kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1461   if (rtp_packet_length < block_pos + header_extension.length ||
1462       rtp_header.headerLength < block_pos + header_extension.length) {
1463     LOG(LS_WARNING) << "Failed to find extension position for " << type
1464                     << " as the length is invalid.";
1465     return false;
1466   }
1467 
1468   // Verify that header contains extension.
1469   if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1470         (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1471     LOG(LS_WARNING) << "Failed to find extension position for " << type
1472                     << "as hdr extension not found.";
1473     return false;
1474   }
1475 
1476   *position = block_pos;
1477   return true;
1478 }
1479 
VerifyExtension(RTPExtensionType extension_type,uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,size_t extension_length_bytes,size_t * extension_offset) const1480 RTPSender::ExtensionStatus RTPSender::VerifyExtension(
1481     RTPExtensionType extension_type,
1482     uint8_t* rtp_packet,
1483     size_t rtp_packet_length,
1484     const RTPHeader& rtp_header,
1485     size_t extension_length_bytes,
1486     size_t* extension_offset) const {
1487   // Get id.
1488   uint8_t id = 0;
1489   if (rtp_header_extension_map_.GetId(extension_type, &id) != 0)
1490     return ExtensionStatus::kNotRegistered;
1491 
1492   size_t block_pos = 0;
1493   if (!FindHeaderExtensionPosition(extension_type, rtp_packet,
1494                                    rtp_packet_length, rtp_header, &block_pos))
1495     return ExtensionStatus::kError;
1496 
1497   // Verify that header contains extension.
1498   if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1499         (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1500     LOG(LS_WARNING)
1501         << "Failed to update absolute send time, hdr extension not found.";
1502     return ExtensionStatus::kError;
1503   }
1504 
1505   // Verify first byte in block.
1506   const uint8_t first_block_byte = (id << 4) + (extension_length_bytes - 2);
1507   if (rtp_packet[block_pos] != first_block_byte)
1508     return ExtensionStatus::kError;
1509 
1510   *extension_offset = block_pos;
1511   return ExtensionStatus::kOk;
1512 }
1513 
UpdateTransmissionTimeOffset(uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,int64_t time_diff_ms) const1514 void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1515                                              size_t rtp_packet_length,
1516                                              const RTPHeader& rtp_header,
1517                                              int64_t time_diff_ms) const {
1518   size_t offset;
1519   CriticalSectionScoped cs(send_critsect_.get());
1520   switch (VerifyExtension(kRtpExtensionTransmissionTimeOffset, rtp_packet,
1521                           rtp_packet_length, rtp_header,
1522                           kTransmissionTimeOffsetLength, &offset)) {
1523     case ExtensionStatus::kNotRegistered:
1524       return;
1525     case ExtensionStatus::kError:
1526       LOG(LS_WARNING) << "Failed to update transmission time offset.";
1527       return;
1528     case ExtensionStatus::kOk:
1529       break;
1530     default:
1531       RTC_NOTREACHED();
1532   }
1533 
1534   // Update transmission offset field (converting to a 90 kHz timestamp).
1535   ByteWriter<int32_t, 3>::WriteBigEndian(rtp_packet + offset + 1,
1536                                          time_diff_ms * 90);  // RTP timestamp.
1537 }
1538 
UpdateAudioLevel(uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,bool is_voiced,uint8_t dBov) const1539 bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1540                                  size_t rtp_packet_length,
1541                                  const RTPHeader& rtp_header,
1542                                  bool is_voiced,
1543                                  uint8_t dBov) const {
1544   size_t offset;
1545   CriticalSectionScoped cs(send_critsect_.get());
1546 
1547   switch (VerifyExtension(kRtpExtensionAudioLevel, rtp_packet,
1548                           rtp_packet_length, rtp_header, kAudioLevelLength,
1549                           &offset)) {
1550     case ExtensionStatus::kNotRegistered:
1551       return false;
1552     case ExtensionStatus::kError:
1553       LOG(LS_WARNING) << "Failed to update audio level.";
1554       return false;
1555     case ExtensionStatus::kOk:
1556       break;
1557     default:
1558       RTC_NOTREACHED();
1559   }
1560 
1561   rtp_packet[offset + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1562   return true;
1563 }
1564 
UpdateVideoRotation(uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,VideoRotation rotation) const1565 bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1566                                     size_t rtp_packet_length,
1567                                     const RTPHeader& rtp_header,
1568                                     VideoRotation rotation) const {
1569   size_t offset;
1570   CriticalSectionScoped cs(send_critsect_.get());
1571 
1572   switch (VerifyExtension(kRtpExtensionVideoRotation, rtp_packet,
1573                           rtp_packet_length, rtp_header, kVideoRotationLength,
1574                           &offset)) {
1575     case ExtensionStatus::kNotRegistered:
1576       return false;
1577     case ExtensionStatus::kError:
1578       LOG(LS_WARNING) << "Failed to update CVO.";
1579       return false;
1580     case ExtensionStatus::kOk:
1581       break;
1582     default:
1583       RTC_NOTREACHED();
1584   }
1585 
1586   rtp_packet[offset + 1] = ConvertVideoRotationToCVOByte(rotation);
1587   return true;
1588 }
1589 
UpdateAbsoluteSendTime(uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header,int64_t now_ms) const1590 void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1591                                        size_t rtp_packet_length,
1592                                        const RTPHeader& rtp_header,
1593                                        int64_t now_ms) const {
1594   size_t offset;
1595   CriticalSectionScoped cs(send_critsect_.get());
1596 
1597   switch (VerifyExtension(kRtpExtensionAbsoluteSendTime, rtp_packet,
1598                           rtp_packet_length, rtp_header,
1599                           kAbsoluteSendTimeLength, &offset)) {
1600     case ExtensionStatus::kNotRegistered:
1601       return;
1602     case ExtensionStatus::kError:
1603       LOG(LS_WARNING) << "Failed to update absolute send time";
1604       return;
1605     case ExtensionStatus::kOk:
1606       break;
1607     default:
1608       RTC_NOTREACHED();
1609   }
1610 
1611   // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1612   // fractional part).
1613   ByteWriter<uint32_t, 3>::WriteBigEndian(rtp_packet + offset + 1,
1614                                           ConvertMsTo24Bits(now_ms));
1615 }
1616 
UpdateTransportSequenceNumber(uint8_t * rtp_packet,size_t rtp_packet_length,const RTPHeader & rtp_header) const1617 uint16_t RTPSender::UpdateTransportSequenceNumber(
1618     uint8_t* rtp_packet,
1619     size_t rtp_packet_length,
1620     const RTPHeader& rtp_header) const {
1621   size_t offset;
1622   CriticalSectionScoped cs(send_critsect_.get());
1623 
1624   switch (VerifyExtension(kRtpExtensionTransportSequenceNumber, rtp_packet,
1625                           rtp_packet_length, rtp_header,
1626                           kTransportSequenceNumberLength, &offset)) {
1627     case ExtensionStatus::kNotRegistered:
1628       return 0;
1629     case ExtensionStatus::kError:
1630       LOG(LS_WARNING) << "Failed to update transport sequence number";
1631       return 0;
1632     case ExtensionStatus::kOk:
1633       break;
1634     default:
1635       RTC_NOTREACHED();
1636   }
1637 
1638   uint16_t seq = transport_sequence_number_allocator_->AllocateSequenceNumber();
1639   BuildTransportSequenceNumberExtension(rtp_packet + offset, seq);
1640   return seq;
1641 }
1642 
SetSendingStatus(bool enabled)1643 void RTPSender::SetSendingStatus(bool enabled) {
1644   if (enabled) {
1645     uint32_t frequency_hz = SendPayloadFrequency();
1646     uint32_t RTPtime = CurrentRtp(*clock_, frequency_hz);
1647 
1648     // Will be ignored if it's already configured via API.
1649     SetStartTimestamp(RTPtime, false);
1650   } else {
1651     CriticalSectionScoped lock(send_critsect_.get());
1652     if (!ssrc_forced_) {
1653       // Generate a new SSRC.
1654       ssrc_db_.ReturnSSRC(ssrc_);
1655       ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
1656       bitrates_->set_ssrc(ssrc_);
1657     }
1658     // Don't initialize seq number if SSRC passed externally.
1659     if (!sequence_number_forced_ && !ssrc_forced_) {
1660       // Generate a new sequence number.
1661       sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
1662     }
1663   }
1664 }
1665 
SetSendingMediaStatus(bool enabled)1666 void RTPSender::SetSendingMediaStatus(bool enabled) {
1667   CriticalSectionScoped cs(send_critsect_.get());
1668   sending_media_ = enabled;
1669 }
1670 
SendingMedia() const1671 bool RTPSender::SendingMedia() const {
1672   CriticalSectionScoped cs(send_critsect_.get());
1673   return sending_media_;
1674 }
1675 
Timestamp() const1676 uint32_t RTPSender::Timestamp() const {
1677   CriticalSectionScoped cs(send_critsect_.get());
1678   return timestamp_;
1679 }
1680 
SetStartTimestamp(uint32_t timestamp,bool force)1681 void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
1682   CriticalSectionScoped cs(send_critsect_.get());
1683   if (force) {
1684     start_timestamp_forced_ = true;
1685     start_timestamp_ = timestamp;
1686   } else {
1687     if (!start_timestamp_forced_) {
1688       start_timestamp_ = timestamp;
1689     }
1690   }
1691 }
1692 
StartTimestamp() const1693 uint32_t RTPSender::StartTimestamp() const {
1694   CriticalSectionScoped cs(send_critsect_.get());
1695   return start_timestamp_;
1696 }
1697 
GenerateNewSSRC()1698 uint32_t RTPSender::GenerateNewSSRC() {
1699   // If configured via API, return 0.
1700   CriticalSectionScoped cs(send_critsect_.get());
1701 
1702   if (ssrc_forced_) {
1703     return 0;
1704   }
1705   ssrc_ = ssrc_db_.CreateSSRC();  // Can't be 0.
1706   bitrates_->set_ssrc(ssrc_);
1707   return ssrc_;
1708 }
1709 
SetSSRC(uint32_t ssrc)1710 void RTPSender::SetSSRC(uint32_t ssrc) {
1711   // This is configured via the API.
1712   CriticalSectionScoped cs(send_critsect_.get());
1713 
1714   if (ssrc_ == ssrc && ssrc_forced_) {
1715     return;  // Since it's same ssrc, don't reset anything.
1716   }
1717   ssrc_forced_ = true;
1718   ssrc_db_.ReturnSSRC(ssrc_);
1719   ssrc_db_.RegisterSSRC(ssrc);
1720   ssrc_ = ssrc;
1721   bitrates_->set_ssrc(ssrc_);
1722   if (!sequence_number_forced_) {
1723     sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
1724   }
1725 }
1726 
SSRC() const1727 uint32_t RTPSender::SSRC() const {
1728   CriticalSectionScoped cs(send_critsect_.get());
1729   return ssrc_;
1730 }
1731 
SetCsrcs(const std::vector<uint32_t> & csrcs)1732 void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1733   assert(csrcs.size() <= kRtpCsrcSize);
1734   CriticalSectionScoped cs(send_critsect_.get());
1735   csrcs_ = csrcs;
1736 }
1737 
SetSequenceNumber(uint16_t seq)1738 void RTPSender::SetSequenceNumber(uint16_t seq) {
1739   CriticalSectionScoped cs(send_critsect_.get());
1740   sequence_number_forced_ = true;
1741   sequence_number_ = seq;
1742 }
1743 
SequenceNumber() const1744 uint16_t RTPSender::SequenceNumber() const {
1745   CriticalSectionScoped cs(send_critsect_.get());
1746   return sequence_number_;
1747 }
1748 
1749 // Audio.
SendTelephoneEvent(uint8_t key,uint16_t time_ms,uint8_t level)1750 int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1751                                       uint16_t time_ms,
1752                                       uint8_t level) {
1753   if (!audio_configured_) {
1754     return -1;
1755   }
1756   return audio_->SendTelephoneEvent(key, time_ms, level);
1757 }
1758 
SetAudioPacketSize(uint16_t packet_size_samples)1759 int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
1760   if (!audio_configured_) {
1761     return -1;
1762   }
1763   return audio_->SetAudioPacketSize(packet_size_samples);
1764 }
1765 
SetAudioLevel(uint8_t level_d_bov)1766 int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
1767   return audio_->SetAudioLevel(level_d_bov);
1768 }
1769 
SetRED(int8_t payload_type)1770 int32_t RTPSender::SetRED(int8_t payload_type) {
1771   if (!audio_configured_) {
1772     return -1;
1773   }
1774   return audio_->SetRED(payload_type);
1775 }
1776 
RED(int8_t * payload_type) const1777 int32_t RTPSender::RED(int8_t *payload_type) const {
1778   if (!audio_configured_) {
1779     return -1;
1780   }
1781   return audio_->RED(payload_type);
1782 }
1783 
VideoCodecType() const1784 RtpVideoCodecTypes RTPSender::VideoCodecType() const {
1785   assert(!audio_configured_ && "Sender is an audio stream!");
1786   return video_->VideoCodecType();
1787 }
1788 
MaxConfiguredBitrateVideo() const1789 uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
1790   if (audio_configured_) {
1791     return 0;
1792   }
1793   return video_->MaxConfiguredBitrateVideo();
1794 }
1795 
SetGenericFECStatus(bool enable,uint8_t payload_type_red,uint8_t payload_type_fec)1796 void RTPSender::SetGenericFECStatus(bool enable,
1797                                     uint8_t payload_type_red,
1798                                     uint8_t payload_type_fec) {
1799   RTC_DCHECK(!audio_configured_);
1800   video_->SetGenericFECStatus(enable, payload_type_red, payload_type_fec);
1801 }
1802 
GenericFECStatus(bool * enable,uint8_t * payload_type_red,uint8_t * payload_type_fec) const1803 void RTPSender::GenericFECStatus(bool* enable,
1804                                     uint8_t* payload_type_red,
1805                                     uint8_t* payload_type_fec) const {
1806   RTC_DCHECK(!audio_configured_);
1807   video_->GenericFECStatus(enable, payload_type_red, payload_type_fec);
1808 }
1809 
SetFecParameters(const FecProtectionParams * delta_params,const FecProtectionParams * key_params)1810 int32_t RTPSender::SetFecParameters(
1811     const FecProtectionParams *delta_params,
1812     const FecProtectionParams *key_params) {
1813   if (audio_configured_) {
1814     return -1;
1815   }
1816   video_->SetFecParameters(delta_params, key_params);
1817   return 0;
1818 }
1819 
BuildRtxPacket(uint8_t * buffer,size_t * length,uint8_t * buffer_rtx)1820 void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
1821                                uint8_t* buffer_rtx) {
1822   CriticalSectionScoped cs(send_critsect_.get());
1823   uint8_t* data_buffer_rtx = buffer_rtx;
1824   // Add RTX header.
1825   RtpUtility::RtpHeaderParser rtp_parser(
1826       reinterpret_cast<const uint8_t*>(buffer), *length);
1827 
1828   RTPHeader rtp_header;
1829   rtp_parser.Parse(&rtp_header);
1830 
1831   // Add original RTP header.
1832   memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
1833 
1834   // Replace payload type, if a specific type is set for RTX.
1835   if (rtx_payload_type_ != -1) {
1836     data_buffer_rtx[1] = static_cast<uint8_t>(rtx_payload_type_);
1837     if (rtp_header.markerBit)
1838       data_buffer_rtx[1] |= kRtpMarkerBitMask;
1839   }
1840 
1841   // Replace sequence number.
1842   uint8_t* ptr = data_buffer_rtx + 2;
1843   ByteWriter<uint16_t>::WriteBigEndian(ptr, sequence_number_rtx_++);
1844 
1845   // Replace SSRC.
1846   ptr += 6;
1847   ByteWriter<uint32_t>::WriteBigEndian(ptr, ssrc_rtx_);
1848 
1849   // Add OSN (original sequence number).
1850   ptr = data_buffer_rtx + rtp_header.headerLength;
1851   ByteWriter<uint16_t>::WriteBigEndian(ptr, rtp_header.sequenceNumber);
1852   ptr += 2;
1853 
1854   // Add original payload data.
1855   memcpy(ptr, buffer + rtp_header.headerLength,
1856          *length - rtp_header.headerLength);
1857   *length += 2;
1858 }
1859 
RegisterRtpStatisticsCallback(StreamDataCountersCallback * callback)1860 void RTPSender::RegisterRtpStatisticsCallback(
1861     StreamDataCountersCallback* callback) {
1862   CriticalSectionScoped cs(statistics_crit_.get());
1863   rtp_stats_callback_ = callback;
1864 }
1865 
GetRtpStatisticsCallback() const1866 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1867   CriticalSectionScoped cs(statistics_crit_.get());
1868   return rtp_stats_callback_;
1869 }
1870 
BitrateSent() const1871 uint32_t RTPSender::BitrateSent() const {
1872   return total_bitrate_sent_.BitrateLast();
1873 }
1874 
SetRtpState(const RtpState & rtp_state)1875 void RTPSender::SetRtpState(const RtpState& rtp_state) {
1876   SetStartTimestamp(rtp_state.start_timestamp, true);
1877   CriticalSectionScoped lock(send_critsect_.get());
1878   sequence_number_ = rtp_state.sequence_number;
1879   sequence_number_forced_ = true;
1880   timestamp_ = rtp_state.timestamp;
1881   capture_time_ms_ = rtp_state.capture_time_ms;
1882   last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1883   media_has_been_sent_ = rtp_state.media_has_been_sent;
1884 }
1885 
GetRtpState() const1886 RtpState RTPSender::GetRtpState() const {
1887   CriticalSectionScoped lock(send_critsect_.get());
1888 
1889   RtpState state;
1890   state.sequence_number = sequence_number_;
1891   state.start_timestamp = start_timestamp_;
1892   state.timestamp = timestamp_;
1893   state.capture_time_ms = capture_time_ms_;
1894   state.last_timestamp_time_ms = last_timestamp_time_ms_;
1895   state.media_has_been_sent = media_has_been_sent_;
1896 
1897   return state;
1898 }
1899 
SetRtxRtpState(const RtpState & rtp_state)1900 void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1901   CriticalSectionScoped lock(send_critsect_.get());
1902   sequence_number_rtx_ = rtp_state.sequence_number;
1903 }
1904 
GetRtxRtpState() const1905 RtpState RTPSender::GetRtxRtpState() const {
1906   CriticalSectionScoped lock(send_critsect_.get());
1907 
1908   RtpState state;
1909   state.sequence_number = sequence_number_rtx_;
1910   state.start_timestamp = start_timestamp_;
1911 
1912   return state;
1913 }
1914 
1915 }  // namespace webrtc
1916