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