OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/video/vie_remb.h" | 11 #include "webrtc/video/vie_remb.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 | 16 |
17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
18 #include "webrtc/modules/utility/include/process_thread.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" | 19 #include "webrtc/system_wrappers/include/tick_util.h" |
21 #include "webrtc/system_wrappers/include/trace.h" | 20 #include "webrtc/system_wrappers/include/trace.h" |
22 | 21 |
23 namespace webrtc { | 22 namespace webrtc { |
24 | 23 |
25 const int kRembSendIntervalMs = 200; | 24 const int kRembSendIntervalMs = 200; |
26 | 25 |
27 // % threshold for if we should send a new REMB asap. | 26 // % threshold for if we should send a new REMB asap. |
28 const unsigned int kSendThresholdPercent = 97; | 27 const unsigned int kSendThresholdPercent = 97; |
29 | 28 |
30 VieRemb::VieRemb(Clock* clock) | 29 VieRemb::VieRemb(Clock* clock) |
31 : clock_(clock), | 30 : clock_(clock), |
32 list_crit_(CriticalSectionWrapper::CreateCriticalSection()), | |
33 last_remb_time_(clock_->TimeInMilliseconds()), | 31 last_remb_time_(clock_->TimeInMilliseconds()), |
34 last_send_bitrate_(0), | 32 last_send_bitrate_(0), |
35 bitrate_(0) {} | 33 bitrate_(0) {} |
36 | 34 |
37 VieRemb::~VieRemb() {} | 35 VieRemb::~VieRemb() {} |
38 | 36 |
39 void VieRemb::AddReceiveChannel(RtpRtcp* rtp_rtcp) { | 37 void VieRemb::AddReceiveChannel(RtpRtcp* rtp_rtcp) { |
40 assert(rtp_rtcp); | 38 assert(rtp_rtcp); |
41 | 39 |
42 CriticalSectionScoped cs(list_crit_.get()); | 40 rtc::CritScope lock(&list_crit_); |
43 if (std::find(receive_modules_.begin(), receive_modules_.end(), rtp_rtcp) != | 41 if (std::find(receive_modules_.begin(), receive_modules_.end(), rtp_rtcp) != |
44 receive_modules_.end()) | 42 receive_modules_.end()) |
45 return; | 43 return; |
46 | 44 |
47 // The module probably doesn't have a remote SSRC yet, so don't add it to the | 45 // The module probably doesn't have a remote SSRC yet, so don't add it to the |
48 // map. | 46 // map. |
49 receive_modules_.push_back(rtp_rtcp); | 47 receive_modules_.push_back(rtp_rtcp); |
50 } | 48 } |
51 | 49 |
52 void VieRemb::RemoveReceiveChannel(RtpRtcp* rtp_rtcp) { | 50 void VieRemb::RemoveReceiveChannel(RtpRtcp* rtp_rtcp) { |
53 assert(rtp_rtcp); | 51 assert(rtp_rtcp); |
54 | 52 |
55 CriticalSectionScoped cs(list_crit_.get()); | 53 rtc::CritScope lock(&list_crit_); |
56 for (RtpModules::iterator it = receive_modules_.begin(); | 54 for (RtpModules::iterator it = receive_modules_.begin(); |
57 it != receive_modules_.end(); ++it) { | 55 it != receive_modules_.end(); ++it) { |
58 if ((*it) == rtp_rtcp) { | 56 if ((*it) == rtp_rtcp) { |
59 receive_modules_.erase(it); | 57 receive_modules_.erase(it); |
60 break; | 58 break; |
61 } | 59 } |
62 } | 60 } |
63 } | 61 } |
64 | 62 |
65 void VieRemb::AddRembSender(RtpRtcp* rtp_rtcp) { | 63 void VieRemb::AddRembSender(RtpRtcp* rtp_rtcp) { |
66 assert(rtp_rtcp); | 64 assert(rtp_rtcp); |
67 | 65 |
68 CriticalSectionScoped cs(list_crit_.get()); | 66 rtc::CritScope lock(&list_crit_); |
69 | 67 |
70 // Verify this module hasn't been added earlier. | 68 // Verify this module hasn't been added earlier. |
71 if (std::find(rtcp_sender_.begin(), rtcp_sender_.end(), rtp_rtcp) != | 69 if (std::find(rtcp_sender_.begin(), rtcp_sender_.end(), rtp_rtcp) != |
72 rtcp_sender_.end()) | 70 rtcp_sender_.end()) |
73 return; | 71 return; |
74 rtcp_sender_.push_back(rtp_rtcp); | 72 rtcp_sender_.push_back(rtp_rtcp); |
75 } | 73 } |
76 | 74 |
77 void VieRemb::RemoveRembSender(RtpRtcp* rtp_rtcp) { | 75 void VieRemb::RemoveRembSender(RtpRtcp* rtp_rtcp) { |
78 assert(rtp_rtcp); | 76 assert(rtp_rtcp); |
79 | 77 |
80 CriticalSectionScoped cs(list_crit_.get()); | 78 rtc::CritScope lock(&list_crit_); |
81 for (RtpModules::iterator it = rtcp_sender_.begin(); | 79 for (RtpModules::iterator it = rtcp_sender_.begin(); |
82 it != rtcp_sender_.end(); ++it) { | 80 it != rtcp_sender_.end(); ++it) { |
83 if ((*it) == rtp_rtcp) { | 81 if ((*it) == rtp_rtcp) { |
84 rtcp_sender_.erase(it); | 82 rtcp_sender_.erase(it); |
85 return; | 83 return; |
86 } | 84 } |
87 } | 85 } |
88 } | 86 } |
89 | 87 |
90 bool VieRemb::InUse() const { | 88 bool VieRemb::InUse() const { |
91 CriticalSectionScoped cs(list_crit_.get()); | 89 rtc::CritScope lock(&list_crit_); |
92 if (receive_modules_.empty() && rtcp_sender_.empty()) | 90 return !receive_modules_.empty() || !rtcp_sender_.empty(); |
93 return false; | |
94 else | |
95 return true; | |
96 } | 91 } |
97 | 92 |
98 void VieRemb::OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs, | 93 void VieRemb::OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs, |
99 unsigned int bitrate) { | 94 unsigned int bitrate) { |
100 list_crit_->Enter(); | 95 RtpRtcp* sender = NULL; |
101 // If we already have an estimate, check if the new total estimate is below | 96 { |
102 // kSendThresholdPercent of the previous estimate. | 97 rtc::CritScope lock(&list_crit_); |
103 if (last_send_bitrate_ > 0) { | 98 // If we already have an estimate, check if the new total estimate is below |
104 unsigned int new_remb_bitrate = last_send_bitrate_ - bitrate_ + bitrate; | 99 // kSendThresholdPercent of the previous estimate. |
| 100 if (last_send_bitrate_ > 0) { |
| 101 unsigned int new_remb_bitrate = last_send_bitrate_ - bitrate_ + bitrate; |
105 | 102 |
106 if (new_remb_bitrate < kSendThresholdPercent * last_send_bitrate_ / 100) { | 103 if (new_remb_bitrate < kSendThresholdPercent * last_send_bitrate_ / 100) { |
107 // The new bitrate estimate is less than kSendThresholdPercent % of the | 104 // The new bitrate estimate is less than kSendThresholdPercent % of the |
108 // last report. Send a REMB asap. | 105 // last report. Send a REMB asap. |
109 last_remb_time_ = clock_->TimeInMilliseconds() - kRembSendIntervalMs; | 106 last_remb_time_ = clock_->TimeInMilliseconds() - kRembSendIntervalMs; |
| 107 } |
110 } | 108 } |
| 109 bitrate_ = bitrate; |
| 110 |
| 111 // Calculate total receive bitrate estimate. |
| 112 int64_t now = clock_->TimeInMilliseconds(); |
| 113 |
| 114 if (now - last_remb_time_ < kRembSendIntervalMs) { |
| 115 return; |
| 116 } |
| 117 last_remb_time_ = now; |
| 118 |
| 119 if (ssrcs.empty() || receive_modules_.empty()) { |
| 120 return; |
| 121 } |
| 122 |
| 123 // Send a REMB packet. |
| 124 if (!rtcp_sender_.empty()) { |
| 125 sender = rtcp_sender_.front(); |
| 126 } else { |
| 127 sender = receive_modules_.front(); |
| 128 } |
| 129 last_send_bitrate_ = bitrate_; |
111 } | 130 } |
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 | 131 |
139 if (sender) { | 132 if (sender) { |
140 sender->SetREMBData(bitrate_, ssrcs); | 133 sender->SetREMBData(bitrate_, ssrcs); |
141 } | 134 } |
142 } | 135 } |
143 | 136 |
144 } // namespace webrtc | 137 } // namespace webrtc |
OLD | NEW |