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