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/modules/rtp_rtcp/source/rtp_sender.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h" |
12 | 12 |
13 #include <stdlib.h> // srand | 13 #include <stdlib.h> // srand |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
19 #include "webrtc/base/rate_limiter.h" | |
20 #include "webrtc/base/trace_event.h" | 19 #include "webrtc/base/trace_event.h" |
21 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
22 #include "webrtc/call.h" | 21 #include "webrtc/call.h" |
23 #include "webrtc/call/rtc_event_log.h" | 22 #include "webrtc/call/rtc_event_log.h" |
24 #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h" | 23 #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h" |
25 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 24 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
26 #include "webrtc/modules/rtp_rtcp/source/playout_delay_oracle.h" | 25 #include "webrtc/modules/rtp_rtcp/source/playout_delay_oracle.h" |
27 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h" | 26 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h" |
28 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h" | 27 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h" |
29 #include "webrtc/modules/rtp_rtcp/source/time_util.h" | 28 #include "webrtc/modules/rtp_rtcp/source/time_util.h" |
30 | 29 |
31 namespace webrtc { | 30 namespace webrtc { |
32 | 31 |
33 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP. | 32 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP. |
34 static const size_t kMaxPaddingLength = 224; | 33 static const size_t kMaxPaddingLength = 224; |
35 static const int kSendSideDelayWindowMs = 1000; | 34 static const int kSendSideDelayWindowMs = 1000; |
36 static const uint32_t kAbsSendTimeFraction = 18; | 35 static const uint32_t kAbsSendTimeFraction = 18; |
37 static const int kBitrateStatisticsWindowMs = 1000; | |
38 | 36 |
39 namespace { | 37 namespace { |
40 | 38 |
41 const size_t kRtpHeaderLength = 12; | 39 const size_t kRtpHeaderLength = 12; |
42 const uint16_t kMaxInitRtpSeqNumber = 32767; // 2^15 -1. | 40 const uint16_t kMaxInitRtpSeqNumber = 32767; // 2^15 -1. |
43 | 41 |
44 const char* FrameTypeToString(FrameType frame_type) { | 42 const char* FrameTypeToString(FrameType frame_type) { |
45 switch (frame_type) { | 43 switch (frame_type) { |
46 case kEmptyFrame: | 44 case kEmptyFrame: |
47 return "empty"; | 45 return "empty"; |
(...skipping 10 matching lines...) Expand all Loading... |
58 uint32_t ConvertMsTo24Bits(int64_t time_ms) { | 56 uint32_t ConvertMsTo24Bits(int64_t time_ms) { |
59 uint32_t time_24_bits = | 57 uint32_t time_24_bits = |
60 static_cast<uint32_t>( | 58 static_cast<uint32_t>( |
61 ((static_cast<uint64_t>(time_ms) << kAbsSendTimeFraction) + 500) / | 59 ((static_cast<uint64_t>(time_ms) << kAbsSendTimeFraction) + 500) / |
62 1000) & | 60 1000) & |
63 0x00FFFFFF; | 61 0x00FFFFFF; |
64 return time_24_bits; | 62 return time_24_bits; |
65 } | 63 } |
66 } // namespace | 64 } // namespace |
67 | 65 |
| 66 RTPSender::BitrateAggregator::BitrateAggregator( |
| 67 BitrateStatisticsObserver* bitrate_callback) |
| 68 : callback_(bitrate_callback), |
| 69 total_bitrate_observer_(*this), |
| 70 retransmit_bitrate_observer_(*this), |
| 71 ssrc_(0) {} |
| 72 |
| 73 void RTPSender::BitrateAggregator::OnStatsUpdated() const { |
| 74 if (callback_) { |
| 75 callback_->Notify(total_bitrate_observer_.statistics(), |
| 76 retransmit_bitrate_observer_.statistics(), ssrc_); |
| 77 } |
| 78 } |
| 79 |
| 80 Bitrate::Observer* RTPSender::BitrateAggregator::total_bitrate_observer() { |
| 81 return &total_bitrate_observer_; |
| 82 } |
| 83 Bitrate::Observer* RTPSender::BitrateAggregator::retransmit_bitrate_observer() { |
| 84 return &retransmit_bitrate_observer_; |
| 85 } |
| 86 |
| 87 void RTPSender::BitrateAggregator::set_ssrc(uint32_t ssrc) { |
| 88 ssrc_ = ssrc; |
| 89 } |
| 90 |
| 91 RTPSender::BitrateAggregator::BitrateObserver::BitrateObserver( |
| 92 const BitrateAggregator& aggregator) |
| 93 : aggregator_(aggregator) {} |
| 94 |
| 95 // Implements Bitrate::Observer. |
| 96 void RTPSender::BitrateAggregator::BitrateObserver::BitrateUpdated( |
| 97 const BitrateStatistics& stats) { |
| 98 statistics_ = stats; |
| 99 aggregator_.OnStatsUpdated(); |
| 100 } |
| 101 |
| 102 const BitrateStatistics& |
| 103 RTPSender::BitrateAggregator::BitrateObserver::statistics() const { |
| 104 return statistics_; |
| 105 } |
| 106 |
68 RTPSender::RTPSender( | 107 RTPSender::RTPSender( |
69 bool audio, | 108 bool audio, |
70 Clock* clock, | 109 Clock* clock, |
71 Transport* transport, | 110 Transport* transport, |
72 RtpPacketSender* paced_sender, | 111 RtpPacketSender* paced_sender, |
73 TransportSequenceNumberAllocator* sequence_number_allocator, | 112 TransportSequenceNumberAllocator* sequence_number_allocator, |
74 TransportFeedbackObserver* transport_feedback_observer, | 113 TransportFeedbackObserver* transport_feedback_observer, |
75 BitrateStatisticsObserver* bitrate_callback, | 114 BitrateStatisticsObserver* bitrate_callback, |
76 FrameCountObserver* frame_count_observer, | 115 FrameCountObserver* frame_count_observer, |
77 SendSideDelayObserver* send_side_delay_observer, | 116 SendSideDelayObserver* send_side_delay_observer, |
78 RtcEventLog* event_log, | 117 RtcEventLog* event_log, |
79 SendPacketObserver* send_packet_observer, | 118 SendPacketObserver* send_packet_observer) |
80 RateLimiter* retransmission_rate_limiter) | |
81 : clock_(clock), | 119 : clock_(clock), |
82 // TODO(holmer): Remove this conversion? | 120 // TODO(holmer): Remove this conversion? |
83 clock_delta_ms_(clock_->TimeInMilliseconds() - rtc::TimeMillis()), | 121 clock_delta_ms_(clock_->TimeInMilliseconds() - rtc::TimeMillis()), |
84 random_(clock_->TimeInMicroseconds()), | 122 random_(clock_->TimeInMicroseconds()), |
| 123 bitrates_(bitrate_callback), |
| 124 total_bitrate_sent_(clock, bitrates_.total_bitrate_observer()), |
85 audio_configured_(audio), | 125 audio_configured_(audio), |
86 audio_(audio ? new RTPSenderAudio(clock, this) : nullptr), | 126 audio_(audio ? new RTPSenderAudio(clock, this) : nullptr), |
87 video_(audio ? nullptr : new RTPSenderVideo(clock, this)), | 127 video_(audio ? nullptr : new RTPSenderVideo(clock, this)), |
88 paced_sender_(paced_sender), | 128 paced_sender_(paced_sender), |
89 transport_sequence_number_allocator_(sequence_number_allocator), | 129 transport_sequence_number_allocator_(sequence_number_allocator), |
90 transport_feedback_observer_(transport_feedback_observer), | 130 transport_feedback_observer_(transport_feedback_observer), |
91 last_capture_time_ms_sent_(0), | 131 last_capture_time_ms_sent_(0), |
92 transport_(transport), | 132 transport_(transport), |
93 sending_media_(true), // Default to sending media. | 133 sending_media_(true), // Default to sending media. |
94 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP. | 134 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP. |
95 payload_type_(-1), | 135 payload_type_(-1), |
96 payload_type_map_(), | 136 payload_type_map_(), |
97 rtp_header_extension_map_(), | 137 rtp_header_extension_map_(), |
98 transmission_time_offset_(0), | 138 transmission_time_offset_(0), |
99 absolute_send_time_(0), | 139 absolute_send_time_(0), |
100 rotation_(kVideoRotation_0), | 140 rotation_(kVideoRotation_0), |
101 video_rotation_active_(false), | 141 video_rotation_active_(false), |
102 transport_sequence_number_(0), | 142 transport_sequence_number_(0), |
| 143 // NACK. |
| 144 nack_byte_count_times_(), |
| 145 nack_byte_count_(), |
| 146 nack_bitrate_(clock, bitrates_.retransmit_bitrate_observer()), |
103 playout_delay_active_(false), | 147 playout_delay_active_(false), |
104 packet_history_(clock), | 148 packet_history_(clock), |
105 // Statistics | 149 // Statistics |
106 rtp_stats_callback_(nullptr), | 150 rtp_stats_callback_(NULL), |
107 total_bitrate_sent_(kBitrateStatisticsWindowMs, | |
108 RateStatistics::kBpsScale), | |
109 nack_bitrate_sent_(kBitrateStatisticsWindowMs, RateStatistics::kBpsScale), | |
110 frame_count_observer_(frame_count_observer), | 151 frame_count_observer_(frame_count_observer), |
111 send_side_delay_observer_(send_side_delay_observer), | 152 send_side_delay_observer_(send_side_delay_observer), |
112 event_log_(event_log), | 153 event_log_(event_log), |
113 send_packet_observer_(send_packet_observer), | 154 send_packet_observer_(send_packet_observer), |
114 bitrate_callback_(bitrate_callback), | |
115 // RTP variables | 155 // RTP variables |
116 start_timestamp_forced_(false), | 156 start_timestamp_forced_(false), |
117 start_timestamp_(0), | 157 start_timestamp_(0), |
118 ssrc_db_(SSRCDatabase::GetSSRCDatabase()), | 158 ssrc_db_(SSRCDatabase::GetSSRCDatabase()), |
119 remote_ssrc_(0), | 159 remote_ssrc_(0), |
120 sequence_number_forced_(false), | 160 sequence_number_forced_(false), |
121 ssrc_forced_(false), | 161 ssrc_forced_(false), |
122 timestamp_(0), | 162 timestamp_(0), |
123 capture_time_ms_(0), | 163 capture_time_ms_(0), |
124 last_timestamp_time_ms_(0), | 164 last_timestamp_time_ms_(0), |
125 media_has_been_sent_(false), | 165 media_has_been_sent_(false), |
126 last_packet_marker_bit_(false), | 166 last_packet_marker_bit_(false), |
127 csrcs_(), | 167 csrcs_(), |
128 rtx_(kRtxOff), | 168 rtx_(kRtxOff), |
129 retransmission_rate_limiter_(retransmission_rate_limiter) { | 169 target_bitrate_(0) { |
| 170 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_)); |
| 171 memset(nack_byte_count_, 0, sizeof(nack_byte_count_)); |
130 // We need to seed the random generator for BuildPaddingPacket() below. | 172 // We need to seed the random generator for BuildPaddingPacket() below. |
131 // TODO(holmer,tommi): Note that TimeInMilliseconds might return 0 on Mac | 173 // TODO(holmer,tommi): Note that TimeInMilliseconds might return 0 on Mac |
132 // early on in the process. | 174 // early on in the process. |
133 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds())); | 175 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds())); |
134 ssrc_ = ssrc_db_->CreateSSRC(); | 176 ssrc_ = ssrc_db_->CreateSSRC(); |
135 RTC_DCHECK(ssrc_ != 0); | 177 RTC_DCHECK(ssrc_ != 0); |
136 ssrc_rtx_ = ssrc_db_->CreateSSRC(); | 178 ssrc_rtx_ = ssrc_db_->CreateSSRC(); |
137 RTC_DCHECK(ssrc_rtx_ != 0); | 179 RTC_DCHECK(ssrc_rtx_ != 0); |
138 | 180 |
| 181 bitrates_.set_ssrc(ssrc_); |
139 // Random start, 16 bits. Can't be 0. | 182 // Random start, 16 bits. Can't be 0. |
140 sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 183 sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
141 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 184 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
142 } | 185 } |
143 | 186 |
144 RTPSender::~RTPSender() { | 187 RTPSender::~RTPSender() { |
145 // TODO(tommi): Use a thread checker to ensure the object is created and | 188 // TODO(tommi): Use a thread checker to ensure the object is created and |
146 // deleted on the same thread. At the moment this isn't possible due to | 189 // deleted on the same thread. At the moment this isn't possible due to |
147 // voe::ChannelOwner in voice engine. To reproduce, run: | 190 // voe::ChannelOwner in voice engine. To reproduce, run: |
148 // voe_auto_test --automated --gtest_filter=*MixManyChannelsForStressOpus | 191 // voe_auto_test --automated --gtest_filter=*MixManyChannelsForStressOpus |
149 | 192 |
150 // TODO(tommi,holmer): We don't grab locks in the dtor before accessing member | 193 // TODO(tommi,holmer): We don't grab locks in the dtor before accessing member |
151 // variables but we grab them in all other methods. (what's the design?) | 194 // variables but we grab them in all other methods. (what's the design?) |
152 // Start documenting what thread we're on in what method so that it's easier | 195 // Start documenting what thread we're on in what method so that it's easier |
153 // to understand performance attributes and possibly remove locks. | 196 // to understand performance attributes and possibly remove locks. |
154 if (remote_ssrc_ != 0) { | 197 if (remote_ssrc_ != 0) { |
155 ssrc_db_->ReturnSSRC(remote_ssrc_); | 198 ssrc_db_->ReturnSSRC(remote_ssrc_); |
156 } | 199 } |
157 ssrc_db_->ReturnSSRC(ssrc_); | 200 ssrc_db_->ReturnSSRC(ssrc_); |
158 | 201 |
159 SSRCDatabase::ReturnSSRCDatabase(); | 202 SSRCDatabase::ReturnSSRCDatabase(); |
160 while (!payload_type_map_.empty()) { | 203 while (!payload_type_map_.empty()) { |
161 std::map<int8_t, RtpUtility::Payload*>::iterator it = | 204 std::map<int8_t, RtpUtility::Payload*>::iterator it = |
162 payload_type_map_.begin(); | 205 payload_type_map_.begin(); |
163 delete it->second; | 206 delete it->second; |
164 payload_type_map_.erase(it); | 207 payload_type_map_.erase(it); |
165 } | 208 } |
166 } | 209 } |
167 | 210 |
| 211 void RTPSender::SetTargetBitrate(uint32_t bitrate) { |
| 212 rtc::CritScope cs(&target_bitrate_critsect_); |
| 213 target_bitrate_ = bitrate; |
| 214 } |
| 215 |
| 216 uint32_t RTPSender::GetTargetBitrate() { |
| 217 rtc::CritScope cs(&target_bitrate_critsect_); |
| 218 return target_bitrate_; |
| 219 } |
| 220 |
168 uint16_t RTPSender::ActualSendBitrateKbit() const { | 221 uint16_t RTPSender::ActualSendBitrateKbit() const { |
169 rtc::CritScope cs(&statistics_crit_); | 222 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000); |
170 return static_cast<uint16_t>( | |
171 total_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0) / | |
172 1000); | |
173 } | 223 } |
174 | 224 |
175 uint32_t RTPSender::VideoBitrateSent() const { | 225 uint32_t RTPSender::VideoBitrateSent() const { |
176 if (video_) { | 226 if (video_) { |
177 return video_->VideoBitrateSent(); | 227 return video_->VideoBitrateSent(); |
178 } | 228 } |
179 return 0; | 229 return 0; |
180 } | 230 } |
181 | 231 |
182 uint32_t RTPSender::FecOverheadRate() const { | 232 uint32_t RTPSender::FecOverheadRate() const { |
183 if (video_) { | 233 if (video_) { |
184 return video_->FecOverheadRate(); | 234 return video_->FecOverheadRate(); |
185 } | 235 } |
186 return 0; | 236 return 0; |
187 } | 237 } |
188 | 238 |
189 uint32_t RTPSender::NackOverheadRate() const { | 239 uint32_t RTPSender::NackOverheadRate() const { |
190 rtc::CritScope cs(&statistics_crit_); | 240 return nack_bitrate_.BitrateLast(); |
191 return nack_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0); | |
192 } | 241 } |
193 | 242 |
194 int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) { | 243 int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) { |
195 if (transmission_time_offset > (0x800000 - 1) || | 244 if (transmission_time_offset > (0x800000 - 1) || |
196 transmission_time_offset < -(0x800000 - 1)) { // Word24. | 245 transmission_time_offset < -(0x800000 - 1)) { // Word24. |
197 return -1; | 246 return -1; |
198 } | 247 } |
199 rtc::CritScope lock(&send_critsect_); | 248 rtc::CritScope lock(&send_critsect_); |
200 transmission_time_offset_ = transmission_time_offset; | 249 transmission_time_offset_ = transmission_time_offset; |
201 return 0; | 250 return 0; |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 uint8_t data_buffer[IP_PACKET_SIZE]; | 747 uint8_t data_buffer[IP_PACKET_SIZE]; |
699 int64_t capture_time_ms; | 748 int64_t capture_time_ms; |
700 | 749 |
701 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true, | 750 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true, |
702 data_buffer, &length, | 751 data_buffer, &length, |
703 &capture_time_ms)) { | 752 &capture_time_ms)) { |
704 // Packet not found. | 753 // Packet not found. |
705 return 0; | 754 return 0; |
706 } | 755 } |
707 | 756 |
708 // Check if we're overusing retransmission bitrate. | |
709 // TODO(sprang): Add histograms for nack success or failure reasons. | |
710 RTC_DCHECK(retransmission_rate_limiter_); | |
711 if (!retransmission_rate_limiter_->TryUseRate(length)) | |
712 return -1; | |
713 | |
714 if (paced_sender_) { | 757 if (paced_sender_) { |
715 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length); | 758 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length); |
716 RTPHeader header; | 759 RTPHeader header; |
717 if (!rtp_parser.Parse(&header)) { | 760 if (!rtp_parser.Parse(&header)) { |
718 assert(false); | 761 assert(false); |
719 return -1; | 762 return -1; |
720 } | 763 } |
721 // Convert from TickTime to Clock since capture_time_ms is based on | 764 // Convert from TickTime to Clock since capture_time_ms is based on |
722 // TickTime. | 765 // TickTime. |
723 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_; | 766 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 return -1; | 817 return -1; |
775 video_->SetSelectiveRetransmissions(settings); | 818 video_->SetSelectiveRetransmissions(settings); |
776 return 0; | 819 return 0; |
777 } | 820 } |
778 | 821 |
779 void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers, | 822 void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers, |
780 int64_t avg_rtt) { | 823 int64_t avg_rtt) { |
781 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), | 824 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), |
782 "RTPSender::OnReceivedNACK", "num_seqnum", | 825 "RTPSender::OnReceivedNACK", "num_seqnum", |
783 nack_sequence_numbers.size(), "avg_rtt", avg_rtt); | 826 nack_sequence_numbers.size(), "avg_rtt", avg_rtt); |
784 for (uint16_t seq_no : nack_sequence_numbers) { | 827 const int64_t now = clock_->TimeInMilliseconds(); |
785 const int32_t bytes_sent = ReSendPacket(seq_no, 5 + avg_rtt); | 828 uint32_t bytes_re_sent = 0; |
786 if (bytes_sent < 0) { | 829 uint32_t target_bitrate = GetTargetBitrate(); |
| 830 |
| 831 // Enough bandwidth to send NACK? |
| 832 if (!ProcessNACKBitRate(now)) { |
| 833 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target " |
| 834 << target_bitrate; |
| 835 return; |
| 836 } |
| 837 |
| 838 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin(); |
| 839 it != nack_sequence_numbers.end(); ++it) { |
| 840 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt); |
| 841 if (bytes_sent > 0) { |
| 842 bytes_re_sent += bytes_sent; |
| 843 } else if (bytes_sent == 0) { |
| 844 // The packet has previously been resent. |
| 845 // Try resending next packet in the list. |
| 846 continue; |
| 847 } else { |
787 // Failed to send one Sequence number. Give up the rest in this nack. | 848 // Failed to send one Sequence number. Give up the rest in this nack. |
788 LOG(LS_WARNING) << "Failed resending RTP packet " << seq_no | 849 LOG(LS_WARNING) << "Failed resending RTP packet " << *it |
789 << ", Discard rest of packets"; | 850 << ", Discard rest of packets"; |
790 break; | 851 break; |
791 } | 852 } |
| 853 // Delay bandwidth estimate (RTT * BW). |
| 854 if (target_bitrate != 0 && avg_rtt) { |
| 855 // kbits/s * ms = bits => bits/8 = bytes |
| 856 size_t target_bytes = |
| 857 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3; |
| 858 if (bytes_re_sent > target_bytes) { |
| 859 break; // Ignore the rest of the packets in the list. |
| 860 } |
| 861 } |
| 862 } |
| 863 if (bytes_re_sent > 0) { |
| 864 UpdateNACKBitRate(bytes_re_sent, now); |
792 } | 865 } |
793 } | 866 } |
794 | 867 |
795 void RTPSender::OnReceivedRtcpReportBlocks( | 868 void RTPSender::OnReceivedRtcpReportBlocks( |
796 const ReportBlockList& report_blocks) { | 869 const ReportBlockList& report_blocks) { |
797 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks); | 870 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks); |
798 } | 871 } |
799 | 872 |
| 873 bool RTPSender::ProcessNACKBitRate(uint32_t now) { |
| 874 uint32_t num = 0; |
| 875 size_t byte_count = 0; |
| 876 const uint32_t kAvgIntervalMs = 1000; |
| 877 uint32_t target_bitrate = GetTargetBitrate(); |
| 878 |
| 879 rtc::CritScope lock(&send_critsect_); |
| 880 |
| 881 if (target_bitrate == 0) { |
| 882 return true; |
| 883 } |
| 884 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) { |
| 885 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) { |
| 886 // Don't use data older than 1sec. |
| 887 break; |
| 888 } else { |
| 889 byte_count += nack_byte_count_[num]; |
| 890 } |
| 891 } |
| 892 uint32_t time_interval = kAvgIntervalMs; |
| 893 if (num == NACK_BYTECOUNT_SIZE) { |
| 894 // More than NACK_BYTECOUNT_SIZE nack messages has been received |
| 895 // during the last msg_interval. |
| 896 if (nack_byte_count_times_[num - 1] <= now) { |
| 897 time_interval = now - nack_byte_count_times_[num - 1]; |
| 898 } |
| 899 } |
| 900 return (byte_count * 8) < (target_bitrate / 1000 * time_interval); |
| 901 } |
| 902 |
| 903 void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) { |
| 904 rtc::CritScope lock(&send_critsect_); |
| 905 if (bytes == 0) |
| 906 return; |
| 907 nack_bitrate_.Update(bytes); |
| 908 // Save bitrate statistics. |
| 909 // Shift all but first time. |
| 910 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) { |
| 911 nack_byte_count_[i + 1] = nack_byte_count_[i]; |
| 912 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i]; |
| 913 } |
| 914 nack_byte_count_[0] = bytes; |
| 915 nack_byte_count_times_[0] = now; |
| 916 } |
| 917 |
800 // Called from pacer when we can send the packet. | 918 // Called from pacer when we can send the packet. |
801 bool RTPSender::TimeToSendPacket(uint16_t sequence_number, | 919 bool RTPSender::TimeToSendPacket(uint16_t sequence_number, |
802 int64_t capture_time_ms, | 920 int64_t capture_time_ms, |
803 bool retransmission, | 921 bool retransmission, |
804 int probe_cluster_id) { | 922 int probe_cluster_id) { |
805 size_t length = IP_PACKET_SIZE; | 923 size_t length = IP_PACKET_SIZE; |
806 uint8_t data_buffer[IP_PACKET_SIZE]; | 924 uint8_t data_buffer[IP_PACKET_SIZE]; |
807 int64_t stored_time_ms; | 925 int64_t stored_time_ms; |
808 | 926 |
809 if (!packet_history_.GetPacketAndSetSendTime(sequence_number, | 927 if (!packet_history_.GetPacketAndSetSendTime(sequence_number, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 } | 1002 } |
885 | 1003 |
886 void RTPSender::UpdateRtpStats(const uint8_t* buffer, | 1004 void RTPSender::UpdateRtpStats(const uint8_t* buffer, |
887 size_t packet_length, | 1005 size_t packet_length, |
888 const RTPHeader& header, | 1006 const RTPHeader& header, |
889 bool is_rtx, | 1007 bool is_rtx, |
890 bool is_retransmit) { | 1008 bool is_retransmit) { |
891 StreamDataCounters* counters; | 1009 StreamDataCounters* counters; |
892 // Get ssrc before taking statistics_crit_ to avoid possible deadlock. | 1010 // Get ssrc before taking statistics_crit_ to avoid possible deadlock. |
893 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC(); | 1011 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC(); |
894 int64_t now_ms = clock_->TimeInMilliseconds(); | |
895 | 1012 |
896 rtc::CritScope lock(&statistics_crit_); | 1013 rtc::CritScope lock(&statistics_crit_); |
897 if (is_rtx) { | 1014 if (is_rtx) { |
898 counters = &rtx_rtp_stats_; | 1015 counters = &rtx_rtp_stats_; |
899 } else { | 1016 } else { |
900 counters = &rtp_stats_; | 1017 counters = &rtp_stats_; |
901 } | 1018 } |
902 | 1019 |
903 total_bitrate_sent_.Update(packet_length, now_ms); | 1020 total_bitrate_sent_.Update(packet_length); |
904 | 1021 |
905 if (counters->first_packet_time_ms == -1) | 1022 if (counters->first_packet_time_ms == -1) { |
906 counters->first_packet_time_ms = clock_->TimeInMilliseconds(); | 1023 counters->first_packet_time_ms = clock_->TimeInMilliseconds(); |
907 | 1024 } |
908 if (IsFecPacket(buffer, header)) | 1025 if (IsFecPacket(buffer, header)) { |
909 counters->fec.AddPacket(packet_length, header); | 1026 counters->fec.AddPacket(packet_length, header); |
910 | 1027 } |
911 if (is_retransmit) { | 1028 if (is_retransmit) { |
912 counters->retransmitted.AddPacket(packet_length, header); | 1029 counters->retransmitted.AddPacket(packet_length, header); |
913 nack_bitrate_sent_.Update(packet_length, now_ms); | |
914 } | 1030 } |
915 | |
916 counters->transmitted.AddPacket(packet_length, header); | 1031 counters->transmitted.AddPacket(packet_length, header); |
917 | 1032 |
918 if (rtp_stats_callback_) | 1033 if (rtp_stats_callback_) { |
919 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc); | 1034 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc); |
| 1035 } |
920 } | 1036 } |
921 | 1037 |
922 bool RTPSender::IsFecPacket(const uint8_t* buffer, | 1038 bool RTPSender::IsFecPacket(const uint8_t* buffer, |
923 const RTPHeader& header) const { | 1039 const RTPHeader& header) const { |
924 if (!video_) { | 1040 if (!video_) { |
925 return false; | 1041 return false; |
926 } | 1042 } |
927 bool fec_enabled; | 1043 bool fec_enabled; |
928 uint8_t pt_red; | 1044 uint8_t pt_red; |
929 uint8_t pt_fec; | 1045 uint8_t pt_fec; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 void RTPSender::UpdateOnSendPacket(int packet_id, | 1173 void RTPSender::UpdateOnSendPacket(int packet_id, |
1058 int64_t capture_time_ms, | 1174 int64_t capture_time_ms, |
1059 uint32_t ssrc) { | 1175 uint32_t ssrc) { |
1060 if (!send_packet_observer_ || capture_time_ms <= 0 || packet_id == -1) | 1176 if (!send_packet_observer_ || capture_time_ms <= 0 || packet_id == -1) |
1061 return; | 1177 return; |
1062 | 1178 |
1063 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc); | 1179 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc); |
1064 } | 1180 } |
1065 | 1181 |
1066 void RTPSender::ProcessBitrate() { | 1182 void RTPSender::ProcessBitrate() { |
1067 if (!bitrate_callback_) | 1183 rtc::CritScope lock(&send_critsect_); |
| 1184 total_bitrate_sent_.Process(); |
| 1185 nack_bitrate_.Process(); |
| 1186 if (audio_configured_) { |
1068 return; | 1187 return; |
1069 int64_t now_ms = clock_->TimeInMilliseconds(); | |
1070 uint32_t ssrc; | |
1071 { | |
1072 rtc::CritScope lock(&send_critsect_); | |
1073 ssrc = ssrc_; | |
1074 } | 1188 } |
1075 | 1189 video_->ProcessBitrate(); |
1076 rtc::CritScope lock(&statistics_crit_); | |
1077 bitrate_callback_->Notify(total_bitrate_sent_.Rate(now_ms).value_or(0), | |
1078 nack_bitrate_sent_.Rate(now_ms).value_or(0), ssrc); | |
1079 } | 1190 } |
1080 | 1191 |
1081 size_t RTPSender::RtpHeaderLength() const { | 1192 size_t RTPSender::RtpHeaderLength() const { |
1082 rtc::CritScope lock(&send_critsect_); | 1193 rtc::CritScope lock(&send_critsect_); |
1083 size_t rtp_header_length = kRtpHeaderLength; | 1194 size_t rtp_header_length = kRtpHeaderLength; |
1084 rtp_header_length += sizeof(uint32_t) * csrcs_.size(); | 1195 rtp_header_length += sizeof(uint32_t) * csrcs_.size(); |
1085 rtp_header_length += RtpHeaderExtensionLength(); | 1196 rtp_header_length += RtpHeaderExtensionLength(); |
1086 return rtp_header_length; | 1197 return rtp_header_length; |
1087 } | 1198 } |
1088 | 1199 |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1628 | 1739 |
1629 // Will be ignored if it's already configured via API. | 1740 // Will be ignored if it's already configured via API. |
1630 SetStartTimestamp(RTPtime, false); | 1741 SetStartTimestamp(RTPtime, false); |
1631 } else { | 1742 } else { |
1632 rtc::CritScope lock(&send_critsect_); | 1743 rtc::CritScope lock(&send_critsect_); |
1633 if (!ssrc_forced_) { | 1744 if (!ssrc_forced_) { |
1634 // Generate a new SSRC. | 1745 // Generate a new SSRC. |
1635 ssrc_db_->ReturnSSRC(ssrc_); | 1746 ssrc_db_->ReturnSSRC(ssrc_); |
1636 ssrc_ = ssrc_db_->CreateSSRC(); | 1747 ssrc_ = ssrc_db_->CreateSSRC(); |
1637 RTC_DCHECK(ssrc_ != 0); | 1748 RTC_DCHECK(ssrc_ != 0); |
| 1749 bitrates_.set_ssrc(ssrc_); |
1638 } | 1750 } |
1639 // Don't initialize seq number if SSRC passed externally. | 1751 // Don't initialize seq number if SSRC passed externally. |
1640 if (!sequence_number_forced_ && !ssrc_forced_) { | 1752 if (!sequence_number_forced_ && !ssrc_forced_) { |
1641 // Generate a new sequence number. | 1753 // Generate a new sequence number. |
1642 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 1754 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
1643 } | 1755 } |
1644 } | 1756 } |
1645 } | 1757 } |
1646 | 1758 |
1647 void RTPSender::SetSendingMediaStatus(bool enabled) { | 1759 void RTPSender::SetSendingMediaStatus(bool enabled) { |
(...skipping 30 matching lines...) Expand all Loading... |
1678 | 1790 |
1679 uint32_t RTPSender::GenerateNewSSRC() { | 1791 uint32_t RTPSender::GenerateNewSSRC() { |
1680 // If configured via API, return 0. | 1792 // If configured via API, return 0. |
1681 rtc::CritScope lock(&send_critsect_); | 1793 rtc::CritScope lock(&send_critsect_); |
1682 | 1794 |
1683 if (ssrc_forced_) { | 1795 if (ssrc_forced_) { |
1684 return 0; | 1796 return 0; |
1685 } | 1797 } |
1686 ssrc_ = ssrc_db_->CreateSSRC(); | 1798 ssrc_ = ssrc_db_->CreateSSRC(); |
1687 RTC_DCHECK(ssrc_ != 0); | 1799 RTC_DCHECK(ssrc_ != 0); |
| 1800 bitrates_.set_ssrc(ssrc_); |
1688 return ssrc_; | 1801 return ssrc_; |
1689 } | 1802 } |
1690 | 1803 |
1691 void RTPSender::SetSSRC(uint32_t ssrc) { | 1804 void RTPSender::SetSSRC(uint32_t ssrc) { |
1692 // This is configured via the API. | 1805 // This is configured via the API. |
1693 rtc::CritScope lock(&send_critsect_); | 1806 rtc::CritScope lock(&send_critsect_); |
1694 | 1807 |
1695 if (ssrc_ == ssrc && ssrc_forced_) { | 1808 if (ssrc_ == ssrc && ssrc_forced_) { |
1696 return; // Since it's same ssrc, don't reset anything. | 1809 return; // Since it's same ssrc, don't reset anything. |
1697 } | 1810 } |
1698 ssrc_forced_ = true; | 1811 ssrc_forced_ = true; |
1699 ssrc_db_->ReturnSSRC(ssrc_); | 1812 ssrc_db_->ReturnSSRC(ssrc_); |
1700 ssrc_db_->RegisterSSRC(ssrc); | 1813 ssrc_db_->RegisterSSRC(ssrc); |
1701 ssrc_ = ssrc; | 1814 ssrc_ = ssrc; |
| 1815 bitrates_.set_ssrc(ssrc_); |
1702 if (!sequence_number_forced_) { | 1816 if (!sequence_number_forced_) { |
1703 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 1817 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
1704 } | 1818 } |
1705 } | 1819 } |
1706 | 1820 |
1707 uint32_t RTPSender::SSRC() const { | 1821 uint32_t RTPSender::SSRC() const { |
1708 rtc::CritScope lock(&send_critsect_); | 1822 rtc::CritScope lock(&send_critsect_); |
1709 return ssrc_; | 1823 return ssrc_; |
1710 } | 1824 } |
1711 | 1825 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1840 rtc::CritScope cs(&statistics_crit_); | 1954 rtc::CritScope cs(&statistics_crit_); |
1841 rtp_stats_callback_ = callback; | 1955 rtp_stats_callback_ = callback; |
1842 } | 1956 } |
1843 | 1957 |
1844 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const { | 1958 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const { |
1845 rtc::CritScope cs(&statistics_crit_); | 1959 rtc::CritScope cs(&statistics_crit_); |
1846 return rtp_stats_callback_; | 1960 return rtp_stats_callback_; |
1847 } | 1961 } |
1848 | 1962 |
1849 uint32_t RTPSender::BitrateSent() const { | 1963 uint32_t RTPSender::BitrateSent() const { |
1850 rtc::CritScope cs(&statistics_crit_); | 1964 return total_bitrate_sent_.BitrateLast(); |
1851 return total_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0); | |
1852 } | 1965 } |
1853 | 1966 |
1854 void RTPSender::SetRtpState(const RtpState& rtp_state) { | 1967 void RTPSender::SetRtpState(const RtpState& rtp_state) { |
1855 rtc::CritScope lock(&send_critsect_); | 1968 rtc::CritScope lock(&send_critsect_); |
1856 sequence_number_ = rtp_state.sequence_number; | 1969 sequence_number_ = rtp_state.sequence_number; |
1857 sequence_number_forced_ = true; | 1970 sequence_number_forced_ = true; |
1858 timestamp_ = rtp_state.timestamp; | 1971 timestamp_ = rtp_state.timestamp; |
1859 capture_time_ms_ = rtp_state.capture_time_ms; | 1972 capture_time_ms_ = rtp_state.capture_time_ms; |
1860 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms; | 1973 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms; |
1861 media_has_been_sent_ = rtp_state.media_has_been_sent; | 1974 media_has_been_sent_ = rtp_state.media_has_been_sent; |
(...skipping 22 matching lines...) Expand all Loading... |
1884 rtc::CritScope lock(&send_critsect_); | 1997 rtc::CritScope lock(&send_critsect_); |
1885 | 1998 |
1886 RtpState state; | 1999 RtpState state; |
1887 state.sequence_number = sequence_number_rtx_; | 2000 state.sequence_number = sequence_number_rtx_; |
1888 state.start_timestamp = start_timestamp_; | 2001 state.start_timestamp = start_timestamp_; |
1889 | 2002 |
1890 return state; | 2003 return state; |
1891 } | 2004 } |
1892 | 2005 |
1893 } // namespace webrtc | 2006 } // namespace webrtc |
OLD | NEW |