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