OLD | NEW |
| (Empty) |
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 | |
20 namespace webrtc { | |
21 | |
22 const int kRembSendIntervalMs = 200; | |
23 | |
24 // % threshold for if we should send a new REMB asap. | |
25 const uint32_t kSendThresholdPercent = 97; | |
26 | |
27 VieRemb::VieRemb(Clock* clock) | |
28 : clock_(clock), | |
29 last_remb_time_(clock_->TimeInMilliseconds()), | |
30 last_send_bitrate_(0), | |
31 bitrate_(0) {} | |
32 | |
33 VieRemb::~VieRemb() {} | |
34 | |
35 void VieRemb::AddReceiveChannel(RtpRtcp* rtp_rtcp) { | |
36 assert(rtp_rtcp); | |
37 | |
38 rtc::CritScope lock(&list_crit_); | |
39 if (std::find(receive_modules_.begin(), receive_modules_.end(), rtp_rtcp) != | |
40 receive_modules_.end()) | |
41 return; | |
42 | |
43 // The module probably doesn't have a remote SSRC yet, so don't add it to the | |
44 // map. | |
45 receive_modules_.push_back(rtp_rtcp); | |
46 } | |
47 | |
48 void VieRemb::RemoveReceiveChannel(RtpRtcp* rtp_rtcp) { | |
49 assert(rtp_rtcp); | |
50 | |
51 rtc::CritScope lock(&list_crit_); | |
52 for (RtpModules::iterator it = receive_modules_.begin(); | |
53 it != receive_modules_.end(); ++it) { | |
54 if ((*it) == rtp_rtcp) { | |
55 receive_modules_.erase(it); | |
56 break; | |
57 } | |
58 } | |
59 } | |
60 | |
61 void VieRemb::AddRembSender(RtpRtcp* rtp_rtcp) { | |
62 assert(rtp_rtcp); | |
63 | |
64 rtc::CritScope lock(&list_crit_); | |
65 | |
66 // Verify this module hasn't been added earlier. | |
67 if (std::find(rtcp_sender_.begin(), rtcp_sender_.end(), rtp_rtcp) != | |
68 rtcp_sender_.end()) | |
69 return; | |
70 rtcp_sender_.push_back(rtp_rtcp); | |
71 } | |
72 | |
73 void VieRemb::RemoveRembSender(RtpRtcp* rtp_rtcp) { | |
74 assert(rtp_rtcp); | |
75 | |
76 rtc::CritScope lock(&list_crit_); | |
77 for (RtpModules::iterator it = rtcp_sender_.begin(); | |
78 it != rtcp_sender_.end(); ++it) { | |
79 if ((*it) == rtp_rtcp) { | |
80 rtcp_sender_.erase(it); | |
81 return; | |
82 } | |
83 } | |
84 } | |
85 | |
86 bool VieRemb::InUse() const { | |
87 rtc::CritScope lock(&list_crit_); | |
88 return !receive_modules_.empty() || !rtcp_sender_.empty(); | |
89 } | |
90 | |
91 void VieRemb::OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, | |
92 uint32_t bitrate) { | |
93 RtpRtcp* sender = nullptr; | |
94 { | |
95 rtc::CritScope lock(&list_crit_); | |
96 // If we already have an estimate, check if the new total estimate is below | |
97 // kSendThresholdPercent of the previous estimate. | |
98 if (last_send_bitrate_ > 0) { | |
99 uint32_t new_remb_bitrate = last_send_bitrate_ - bitrate_ + bitrate; | |
100 | |
101 if (new_remb_bitrate < kSendThresholdPercent * last_send_bitrate_ / 100) { | |
102 // The new bitrate estimate is less than kSendThresholdPercent % of the | |
103 // last report. Send a REMB asap. | |
104 last_remb_time_ = clock_->TimeInMilliseconds() - kRembSendIntervalMs; | |
105 } | |
106 } | |
107 bitrate_ = bitrate; | |
108 | |
109 // Calculate total receive bitrate estimate. | |
110 int64_t now = clock_->TimeInMilliseconds(); | |
111 | |
112 if (now - last_remb_time_ < kRembSendIntervalMs) { | |
113 return; | |
114 } | |
115 last_remb_time_ = now; | |
116 | |
117 if (ssrcs.empty() || receive_modules_.empty()) { | |
118 return; | |
119 } | |
120 | |
121 // Send a REMB packet. | |
122 if (!rtcp_sender_.empty()) { | |
123 sender = rtcp_sender_.front(); | |
124 } else { | |
125 sender = receive_modules_.front(); | |
126 } | |
127 last_send_bitrate_ = bitrate_; | |
128 } | |
129 | |
130 if (sender) { | |
131 sender->SetREMBData(bitrate_, ssrcs); | |
132 } | |
133 } | |
134 | |
135 } // namespace webrtc | |
OLD | NEW |