Home | History | Annotate | Download | only in pacing
      1 /*
      2  *  Copyright (c) 2015 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/pacing/packet_router.h"
     12 
     13 #include "webrtc/base/atomicops.h"
     14 #include "webrtc/base/checks.h"
     15 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
     16 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
     18 
     19 namespace webrtc {
     20 
     21 PacketRouter::PacketRouter() : transport_seq_(0) {
     22 }
     23 
     24 PacketRouter::~PacketRouter() {
     25   RTC_DCHECK(rtp_modules_.empty());
     26 }
     27 
     28 void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) {
     29   rtc::CritScope cs(&modules_lock_);
     30   RTC_DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) ==
     31              rtp_modules_.end());
     32   rtp_modules_.push_back(rtp_module);
     33 }
     34 
     35 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) {
     36   rtc::CritScope cs(&modules_lock_);
     37   auto it = std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module);
     38   RTC_DCHECK(it != rtp_modules_.end());
     39   rtp_modules_.erase(it);
     40 }
     41 
     42 bool PacketRouter::TimeToSendPacket(uint32_t ssrc,
     43                                     uint16_t sequence_number,
     44                                     int64_t capture_timestamp,
     45                                     bool retransmission) {
     46   rtc::CritScope cs(&modules_lock_);
     47   for (auto* rtp_module : rtp_modules_) {
     48     if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) {
     49       return rtp_module->TimeToSendPacket(ssrc, sequence_number,
     50                                           capture_timestamp, retransmission);
     51     }
     52   }
     53   return true;
     54 }
     55 
     56 size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) {
     57   size_t total_bytes_sent = 0;
     58   rtc::CritScope cs(&modules_lock_);
     59   for (RtpRtcp* module : rtp_modules_) {
     60     if (module->SendingMedia()) {
     61       size_t bytes_sent =
     62           module->TimeToSendPadding(bytes_to_send - total_bytes_sent);
     63       total_bytes_sent += bytes_sent;
     64       if (total_bytes_sent >= bytes_to_send)
     65         break;
     66     }
     67   }
     68   return total_bytes_sent;
     69 }
     70 
     71 void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) {
     72   rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number);
     73 }
     74 
     75 uint16_t PacketRouter::AllocateSequenceNumber() {
     76   int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_);
     77   int desired_prev_seq;
     78   int new_seq;
     79   do {
     80     desired_prev_seq = prev_seq;
     81     new_seq = (desired_prev_seq + 1) & 0xFFFF;
     82     // Note: CompareAndSwap returns the actual value of transport_seq at the
     83     // time the CAS operation was executed. Thus, if prev_seq is returned, the
     84     // operation was successful - otherwise we need to retry. Saving the
     85     // return value saves us a load on retry.
     86     prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq,
     87                                               new_seq);
     88   } while (prev_seq != desired_prev_seq);
     89 
     90   return new_seq;
     91 }
     92 
     93 bool PacketRouter::SendFeedback(rtcp::TransportFeedback* packet) {
     94   rtc::CritScope cs(&modules_lock_);
     95   for (auto* rtp_module : rtp_modules_) {
     96     packet->WithPacketSenderSsrc(rtp_module->SSRC());
     97     if (rtp_module->SendFeedbackPacket(*packet))
     98       return true;
     99   }
    100   return false;
    101 }
    102 
    103 }  // namespace webrtc
    104