• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/video_engine/vie_remb.h"
12 
13 #include <assert.h>
14 
15 #include <algorithm>
16 
17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
18 #include "webrtc/modules/utility/interface/process_thread.h"
19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20 #include "webrtc/system_wrappers/interface/tick_util.h"
21 #include "webrtc/system_wrappers/interface/trace.h"
22 
23 namespace webrtc {
24 
25 const int kRembSendIntervalMs = 200;
26 
27 // % threshold for if we should send a new REMB asap.
28 const unsigned int kSendThresholdPercent = 97;
29 
VieRemb()30 VieRemb::VieRemb()
31     : list_crit_(CriticalSectionWrapper::CreateCriticalSection()),
32       last_remb_time_(TickTime::MillisecondTimestamp()),
33       last_send_bitrate_(0),
34       bitrate_(0) {}
35 
~VieRemb()36 VieRemb::~VieRemb() {}
37 
AddReceiveChannel(RtpRtcp * rtp_rtcp)38 void VieRemb::AddReceiveChannel(RtpRtcp* rtp_rtcp) {
39   assert(rtp_rtcp);
40 
41   CriticalSectionScoped cs(list_crit_.get());
42   if (std::find(receive_modules_.begin(), receive_modules_.end(), rtp_rtcp) !=
43       receive_modules_.end())
44     return;
45 
46   // The module probably doesn't have a remote SSRC yet, so don't add it to the
47   // map.
48   receive_modules_.push_back(rtp_rtcp);
49 }
50 
RemoveReceiveChannel(RtpRtcp * rtp_rtcp)51 void VieRemb::RemoveReceiveChannel(RtpRtcp* rtp_rtcp) {
52   assert(rtp_rtcp);
53 
54   CriticalSectionScoped cs(list_crit_.get());
55   for (RtpModules::iterator it = receive_modules_.begin();
56        it != receive_modules_.end(); ++it) {
57     if ((*it) == rtp_rtcp) {
58       receive_modules_.erase(it);
59       break;
60     }
61   }
62 }
63 
AddRembSender(RtpRtcp * rtp_rtcp)64 void VieRemb::AddRembSender(RtpRtcp* rtp_rtcp) {
65   assert(rtp_rtcp);
66 
67   CriticalSectionScoped cs(list_crit_.get());
68 
69   // Verify this module hasn't been added earlier.
70   if (std::find(rtcp_sender_.begin(), rtcp_sender_.end(), rtp_rtcp) !=
71       rtcp_sender_.end())
72     return;
73   rtcp_sender_.push_back(rtp_rtcp);
74 }
75 
RemoveRembSender(RtpRtcp * rtp_rtcp)76 void VieRemb::RemoveRembSender(RtpRtcp* rtp_rtcp) {
77   assert(rtp_rtcp);
78 
79   CriticalSectionScoped cs(list_crit_.get());
80   for (RtpModules::iterator it = rtcp_sender_.begin();
81        it != rtcp_sender_.end(); ++it) {
82     if ((*it) == rtp_rtcp) {
83       rtcp_sender_.erase(it);
84       return;
85     }
86   }
87 }
88 
InUse() const89 bool VieRemb::InUse() const {
90   CriticalSectionScoped cs(list_crit_.get());
91   if (receive_modules_.empty() && rtcp_sender_.empty())
92     return false;
93   else
94     return true;
95 }
96 
OnReceiveBitrateChanged(const std::vector<unsigned int> & ssrcs,unsigned int bitrate)97 void VieRemb::OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
98                                       unsigned int bitrate) {
99   list_crit_->Enter();
100   // If we already have an estimate, check if the new total estimate is below
101   // kSendThresholdPercent of the previous estimate.
102   if (last_send_bitrate_ > 0) {
103     unsigned int new_remb_bitrate = last_send_bitrate_ - bitrate_ + bitrate;
104 
105     if (new_remb_bitrate < kSendThresholdPercent * last_send_bitrate_ / 100) {
106       // The new bitrate estimate is less than kSendThresholdPercent % of the
107       // last report. Send a REMB asap.
108       last_remb_time_ = TickTime::MillisecondTimestamp() - kRembSendIntervalMs;
109     }
110   }
111   bitrate_ = bitrate;
112 
113   // Calculate total receive bitrate estimate.
114   int64_t now = TickTime::MillisecondTimestamp();
115 
116   if (now - last_remb_time_ < kRembSendIntervalMs) {
117     list_crit_->Leave();
118     return;
119   }
120   last_remb_time_ = now;
121 
122   if (ssrcs.empty() || receive_modules_.empty()) {
123     list_crit_->Leave();
124     return;
125   }
126 
127   // Send a REMB packet.
128   RtpRtcp* sender = NULL;
129   if (!rtcp_sender_.empty()) {
130     sender = rtcp_sender_.front();
131   } else {
132     sender = receive_modules_.front();
133   }
134   last_send_bitrate_ = bitrate_;
135 
136   list_crit_->Leave();
137 
138   if (sender) {
139     // TODO(holmer): Change RTP module API to take a const vector reference.
140     sender->SetREMBData(bitrate_, ssrcs.size(), &ssrcs[0]);
141   }
142 }
143 
144 }  // namespace webrtc
145