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