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

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

Issue 2130423002: Revert of Reland Issue 2061423003: Refactor 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"
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
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
(...skipping 10 matching lines...) Expand all
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
168 void RTPSender::SetTargetBitrate(uint32_t bitrate) { 211 void RTPSender::SetTargetBitrate(uint32_t bitrate) {
169 // TODO(sprang): Remove this when dependencies have been updated. 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_;
170 } 219 }
171 220
172 uint16_t RTPSender::ActualSendBitrateKbit() const { 221 uint16_t RTPSender::ActualSendBitrateKbit() const {
173 rtc::CritScope cs(&statistics_crit_); 222 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
174 return static_cast<uint16_t>(
175 total_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0) /
176 1000);
177 } 223 }
178 224
179 uint32_t RTPSender::VideoBitrateSent() const { 225 uint32_t RTPSender::VideoBitrateSent() const {
180 if (video_) { 226 if (video_) {
181 return video_->VideoBitrateSent(); 227 return video_->VideoBitrateSent();
182 } 228 }
183 return 0; 229 return 0;
184 } 230 }
185 231
186 uint32_t RTPSender::FecOverheadRate() const { 232 uint32_t RTPSender::FecOverheadRate() const {
187 if (video_) { 233 if (video_) {
188 return video_->FecOverheadRate(); 234 return video_->FecOverheadRate();
189 } 235 }
190 return 0; 236 return 0;
191 } 237 }
192 238
193 uint32_t RTPSender::NackOverheadRate() const { 239 uint32_t RTPSender::NackOverheadRate() const {
194 rtc::CritScope cs(&statistics_crit_); 240 return nack_bitrate_.BitrateLast();
195 return nack_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0);
196 } 241 }
197 242
198 int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) { 243 int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
199 if (transmission_time_offset > (0x800000 - 1) || 244 if (transmission_time_offset > (0x800000 - 1) ||
200 transmission_time_offset < -(0x800000 - 1)) { // Word24. 245 transmission_time_offset < -(0x800000 - 1)) { // Word24.
201 return -1; 246 return -1;
202 } 247 }
203 rtc::CritScope lock(&send_critsect_); 248 rtc::CritScope lock(&send_critsect_);
204 transmission_time_offset_ = transmission_time_offset; 249 transmission_time_offset_ = transmission_time_offset;
205 return 0; 250 return 0;
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 uint8_t data_buffer[IP_PACKET_SIZE]; 747 uint8_t data_buffer[IP_PACKET_SIZE];
703 int64_t capture_time_ms; 748 int64_t capture_time_ms;
704 749
705 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true, 750 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
706 data_buffer, &length, 751 data_buffer, &length,
707 &capture_time_ms)) { 752 &capture_time_ms)) {
708 // Packet not found. 753 // Packet not found.
709 return 0; 754 return 0;
710 } 755 }
711 756
712 // Check if we're overusing retransmission bitrate.
713 // TODO(sprang): Add histograms for nack success or failure reasons.
714 RTC_DCHECK(retransmission_rate_limiter_);
715 if (!retransmission_rate_limiter_->TryUseRate(length))
716 return -1;
717
718 if (paced_sender_) { 757 if (paced_sender_) {
719 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length); 758 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
720 RTPHeader header; 759 RTPHeader header;
721 if (!rtp_parser.Parse(&header)) { 760 if (!rtp_parser.Parse(&header)) {
722 assert(false); 761 assert(false);
723 return -1; 762 return -1;
724 } 763 }
725 // 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
726 // TickTime. 765 // TickTime.
727 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
778 return -1; 817 return -1;
779 video_->SetSelectiveRetransmissions(settings); 818 video_->SetSelectiveRetransmissions(settings);
780 return 0; 819 return 0;
781 } 820 }
782 821
783 void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers, 822 void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
784 int64_t avg_rtt) { 823 int64_t avg_rtt) {
785 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), 824 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
786 "RTPSender::OnReceivedNACK", "num_seqnum", 825 "RTPSender::OnReceivedNACK", "num_seqnum",
787 nack_sequence_numbers.size(), "avg_rtt", avg_rtt); 826 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
788 for (uint16_t seq_no : nack_sequence_numbers) { 827 const int64_t now = clock_->TimeInMilliseconds();
789 const int32_t bytes_sent = ReSendPacket(seq_no, 5 + avg_rtt); 828 uint32_t bytes_re_sent = 0;
790 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 {
791 // 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.
792 LOG(LS_WARNING) << "Failed resending RTP packet " << seq_no 849 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
793 << ", Discard rest of packets"; 850 << ", Discard rest of packets";
794 break; 851 break;
795 } 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);
796 } 865 }
797 } 866 }
798 867
799 void RTPSender::OnReceivedRtcpReportBlocks( 868 void RTPSender::OnReceivedRtcpReportBlocks(
800 const ReportBlockList& report_blocks) { 869 const ReportBlockList& report_blocks) {
801 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks); 870 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks);
802 } 871 }
803 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
804 // Called from pacer when we can send the packet. 918 // Called from pacer when we can send the packet.
805 bool RTPSender::TimeToSendPacket(uint16_t sequence_number, 919 bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
806 int64_t capture_time_ms, 920 int64_t capture_time_ms,
807 bool retransmission, 921 bool retransmission,
808 int probe_cluster_id) { 922 int probe_cluster_id) {
809 size_t length = IP_PACKET_SIZE; 923 size_t length = IP_PACKET_SIZE;
810 uint8_t data_buffer[IP_PACKET_SIZE]; 924 uint8_t data_buffer[IP_PACKET_SIZE];
811 int64_t stored_time_ms; 925 int64_t stored_time_ms;
812 926
813 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
888 } 1002 }
889 1003
890 void RTPSender::UpdateRtpStats(const uint8_t* buffer, 1004 void RTPSender::UpdateRtpStats(const uint8_t* buffer,
891 size_t packet_length, 1005 size_t packet_length,
892 const RTPHeader& header, 1006 const RTPHeader& header,
893 bool is_rtx, 1007 bool is_rtx,
894 bool is_retransmit) { 1008 bool is_retransmit) {
895 StreamDataCounters* counters; 1009 StreamDataCounters* counters;
896 // Get ssrc before taking statistics_crit_ to avoid possible deadlock. 1010 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
897 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC(); 1011 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
898 int64_t now_ms = clock_->TimeInMilliseconds();
899 1012
900 rtc::CritScope lock(&statistics_crit_); 1013 rtc::CritScope lock(&statistics_crit_);
901 if (is_rtx) { 1014 if (is_rtx) {
902 counters = &rtx_rtp_stats_; 1015 counters = &rtx_rtp_stats_;
903 } else { 1016 } else {
904 counters = &rtp_stats_; 1017 counters = &rtp_stats_;
905 } 1018 }
906 1019
907 total_bitrate_sent_.Update(packet_length, now_ms); 1020 total_bitrate_sent_.Update(packet_length);
908 1021
909 if (counters->first_packet_time_ms == -1) 1022 if (counters->first_packet_time_ms == -1) {
910 counters->first_packet_time_ms = clock_->TimeInMilliseconds(); 1023 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
911 1024 }
912 if (IsFecPacket(buffer, header)) 1025 if (IsFecPacket(buffer, header)) {
913 counters->fec.AddPacket(packet_length, header); 1026 counters->fec.AddPacket(packet_length, header);
914 1027 }
915 if (is_retransmit) { 1028 if (is_retransmit) {
916 counters->retransmitted.AddPacket(packet_length, header); 1029 counters->retransmitted.AddPacket(packet_length, header);
917 nack_bitrate_sent_.Update(packet_length, now_ms);
918 } 1030 }
919
920 counters->transmitted.AddPacket(packet_length, header); 1031 counters->transmitted.AddPacket(packet_length, header);
921 1032
922 if (rtp_stats_callback_) 1033 if (rtp_stats_callback_) {
923 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc); 1034 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
1035 }
924 } 1036 }
925 1037
926 bool RTPSender::IsFecPacket(const uint8_t* buffer, 1038 bool RTPSender::IsFecPacket(const uint8_t* buffer,
927 const RTPHeader& header) const { 1039 const RTPHeader& header) const {
928 if (!video_) { 1040 if (!video_) {
929 return false; 1041 return false;
930 } 1042 }
931 bool fec_enabled; 1043 bool fec_enabled;
932 uint8_t pt_red; 1044 uint8_t pt_red;
933 uint8_t pt_fec; 1045 uint8_t pt_fec;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 void RTPSender::UpdateOnSendPacket(int packet_id, 1173 void RTPSender::UpdateOnSendPacket(int packet_id,
1062 int64_t capture_time_ms, 1174 int64_t capture_time_ms,
1063 uint32_t ssrc) { 1175 uint32_t ssrc) {
1064 if (!send_packet_observer_ || capture_time_ms <= 0 || packet_id == -1) 1176 if (!send_packet_observer_ || capture_time_ms <= 0 || packet_id == -1)
1065 return; 1177 return;
1066 1178
1067 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc); 1179 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc);
1068 } 1180 }
1069 1181
1070 void RTPSender::ProcessBitrate() { 1182 void RTPSender::ProcessBitrate() {
1071 if (!bitrate_callback_) 1183 rtc::CritScope lock(&send_critsect_);
1184 total_bitrate_sent_.Process();
1185 nack_bitrate_.Process();
1186 if (audio_configured_) {
1072 return; 1187 return;
1073 int64_t now_ms = clock_->TimeInMilliseconds();
1074 uint32_t ssrc;
1075 {
1076 rtc::CritScope lock(&send_critsect_);
1077 ssrc = ssrc_;
1078 } 1188 }
1079 1189 video_->ProcessBitrate();
1080 rtc::CritScope lock(&statistics_crit_);
1081 bitrate_callback_->Notify(total_bitrate_sent_.Rate(now_ms).value_or(0),
1082 nack_bitrate_sent_.Rate(now_ms).value_or(0), ssrc);
1083 } 1190 }
1084 1191
1085 size_t RTPSender::RtpHeaderLength() const { 1192 size_t RTPSender::RtpHeaderLength() const {
1086 rtc::CritScope lock(&send_critsect_); 1193 rtc::CritScope lock(&send_critsect_);
1087 size_t rtp_header_length = kRtpHeaderLength; 1194 size_t rtp_header_length = kRtpHeaderLength;
1088 rtp_header_length += sizeof(uint32_t) * csrcs_.size(); 1195 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
1089 rtp_header_length += RtpHeaderExtensionLength(); 1196 rtp_header_length += RtpHeaderExtensionLength();
1090 return rtp_header_length; 1197 return rtp_header_length;
1091 } 1198 }
1092 1199
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
1632 1739
1633 // Will be ignored if it's already configured via API. 1740 // Will be ignored if it's already configured via API.
1634 SetStartTimestamp(RTPtime, false); 1741 SetStartTimestamp(RTPtime, false);
1635 } else { 1742 } else {
1636 rtc::CritScope lock(&send_critsect_); 1743 rtc::CritScope lock(&send_critsect_);
1637 if (!ssrc_forced_) { 1744 if (!ssrc_forced_) {
1638 // Generate a new SSRC. 1745 // Generate a new SSRC.
1639 ssrc_db_->ReturnSSRC(ssrc_); 1746 ssrc_db_->ReturnSSRC(ssrc_);
1640 ssrc_ = ssrc_db_->CreateSSRC(); 1747 ssrc_ = ssrc_db_->CreateSSRC();
1641 RTC_DCHECK(ssrc_ != 0); 1748 RTC_DCHECK(ssrc_ != 0);
1749 bitrates_.set_ssrc(ssrc_);
1642 } 1750 }
1643 // Don't initialize seq number if SSRC passed externally. 1751 // Don't initialize seq number if SSRC passed externally.
1644 if (!sequence_number_forced_ && !ssrc_forced_) { 1752 if (!sequence_number_forced_ && !ssrc_forced_) {
1645 // Generate a new sequence number. 1753 // Generate a new sequence number.
1646 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); 1754 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
1647 } 1755 }
1648 } 1756 }
1649 } 1757 }
1650 1758
1651 void RTPSender::SetSendingMediaStatus(bool enabled) { 1759 void RTPSender::SetSendingMediaStatus(bool enabled) {
(...skipping 30 matching lines...) Expand all
1682 1790
1683 uint32_t RTPSender::GenerateNewSSRC() { 1791 uint32_t RTPSender::GenerateNewSSRC() {
1684 // If configured via API, return 0. 1792 // If configured via API, return 0.
1685 rtc::CritScope lock(&send_critsect_); 1793 rtc::CritScope lock(&send_critsect_);
1686 1794
1687 if (ssrc_forced_) { 1795 if (ssrc_forced_) {
1688 return 0; 1796 return 0;
1689 } 1797 }
1690 ssrc_ = ssrc_db_->CreateSSRC(); 1798 ssrc_ = ssrc_db_->CreateSSRC();
1691 RTC_DCHECK(ssrc_ != 0); 1799 RTC_DCHECK(ssrc_ != 0);
1800 bitrates_.set_ssrc(ssrc_);
1692 return ssrc_; 1801 return ssrc_;
1693 } 1802 }
1694 1803
1695 void RTPSender::SetSSRC(uint32_t ssrc) { 1804 void RTPSender::SetSSRC(uint32_t ssrc) {
1696 // This is configured via the API. 1805 // This is configured via the API.
1697 rtc::CritScope lock(&send_critsect_); 1806 rtc::CritScope lock(&send_critsect_);
1698 1807
1699 if (ssrc_ == ssrc && ssrc_forced_) { 1808 if (ssrc_ == ssrc && ssrc_forced_) {
1700 return; // Since it's same ssrc, don't reset anything. 1809 return; // Since it's same ssrc, don't reset anything.
1701 } 1810 }
1702 ssrc_forced_ = true; 1811 ssrc_forced_ = true;
1703 ssrc_db_->ReturnSSRC(ssrc_); 1812 ssrc_db_->ReturnSSRC(ssrc_);
1704 ssrc_db_->RegisterSSRC(ssrc); 1813 ssrc_db_->RegisterSSRC(ssrc);
1705 ssrc_ = ssrc; 1814 ssrc_ = ssrc;
1815 bitrates_.set_ssrc(ssrc_);
1706 if (!sequence_number_forced_) { 1816 if (!sequence_number_forced_) {
1707 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); 1817 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber);
1708 } 1818 }
1709 } 1819 }
1710 1820
1711 uint32_t RTPSender::SSRC() const { 1821 uint32_t RTPSender::SSRC() const {
1712 rtc::CritScope lock(&send_critsect_); 1822 rtc::CritScope lock(&send_critsect_);
1713 return ssrc_; 1823 return ssrc_;
1714 } 1824 }
1715 1825
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1844 rtc::CritScope cs(&statistics_crit_); 1954 rtc::CritScope cs(&statistics_crit_);
1845 rtp_stats_callback_ = callback; 1955 rtp_stats_callback_ = callback;
1846 } 1956 }
1847 1957
1848 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const { 1958 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1849 rtc::CritScope cs(&statistics_crit_); 1959 rtc::CritScope cs(&statistics_crit_);
1850 return rtp_stats_callback_; 1960 return rtp_stats_callback_;
1851 } 1961 }
1852 1962
1853 uint32_t RTPSender::BitrateSent() const { 1963 uint32_t RTPSender::BitrateSent() const {
1854 rtc::CritScope cs(&statistics_crit_); 1964 return total_bitrate_sent_.BitrateLast();
1855 return total_bitrate_sent_.Rate(clock_->TimeInMilliseconds()).value_or(0);
1856 } 1965 }
1857 1966
1858 void RTPSender::SetRtpState(const RtpState& rtp_state) { 1967 void RTPSender::SetRtpState(const RtpState& rtp_state) {
1859 rtc::CritScope lock(&send_critsect_); 1968 rtc::CritScope lock(&send_critsect_);
1860 sequence_number_ = rtp_state.sequence_number; 1969 sequence_number_ = rtp_state.sequence_number;
1861 sequence_number_forced_ = true; 1970 sequence_number_forced_ = true;
1862 timestamp_ = rtp_state.timestamp; 1971 timestamp_ = rtp_state.timestamp;
1863 capture_time_ms_ = rtp_state.capture_time_ms; 1972 capture_time_ms_ = rtp_state.capture_time_ms;
1864 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms; 1973 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1865 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
1888 rtc::CritScope lock(&send_critsect_); 1997 rtc::CritScope lock(&send_critsect_);
1889 1998
1890 RtpState state; 1999 RtpState state;
1891 state.sequence_number = sequence_number_rtx_; 2000 state.sequence_number = sequence_number_rtx_;
1892 state.start_timestamp = start_timestamp_; 2001 state.start_timestamp = start_timestamp_;
1893 2002
1894 return state; 2003 return state;
1895 } 2004 }
1896 2005
1897 } // namespace webrtc 2006 } // 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