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