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