Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_sender.cc

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

Powered by Google App Engine
This is Rietveld 408576698