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 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 rtp_stats_callback_(nullptr), | 104 rtp_stats_callback_(nullptr), |
105 total_bitrate_sent_(kBitrateStatisticsWindowMs, | 105 total_bitrate_sent_(kBitrateStatisticsWindowMs, |
106 RateStatistics::kBpsScale), | 106 RateStatistics::kBpsScale), |
107 nack_bitrate_sent_(kBitrateStatisticsWindowMs, RateStatistics::kBpsScale), | 107 nack_bitrate_sent_(kBitrateStatisticsWindowMs, RateStatistics::kBpsScale), |
108 frame_count_observer_(frame_count_observer), | 108 frame_count_observer_(frame_count_observer), |
109 send_side_delay_observer_(send_side_delay_observer), | 109 send_side_delay_observer_(send_side_delay_observer), |
110 event_log_(event_log), | 110 event_log_(event_log), |
111 send_packet_observer_(send_packet_observer), | 111 send_packet_observer_(send_packet_observer), |
112 bitrate_callback_(bitrate_callback), | 112 bitrate_callback_(bitrate_callback), |
113 // RTP variables | 113 // RTP variables |
114 ssrc_db_(SSRCDatabase::GetSSRCDatabase()), | |
115 remote_ssrc_(0), | 114 remote_ssrc_(0), |
116 sequence_number_forced_(false), | 115 sequence_number_forced_(false), |
117 ssrc_forced_(false), | |
118 last_rtp_timestamp_(0), | 116 last_rtp_timestamp_(0), |
119 capture_time_ms_(0), | 117 capture_time_ms_(0), |
120 last_timestamp_time_ms_(0), | 118 last_timestamp_time_ms_(0), |
121 media_has_been_sent_(false), | 119 media_has_been_sent_(false), |
122 last_packet_marker_bit_(false), | 120 last_packet_marker_bit_(false), |
123 csrcs_(), | 121 csrcs_(), |
124 rtx_(kRtxOff), | 122 rtx_(kRtxOff), |
125 rtp_overhead_bytes_per_packet_(0), | 123 rtp_overhead_bytes_per_packet_(0), |
126 retransmission_rate_limiter_(retransmission_rate_limiter), | 124 retransmission_rate_limiter_(retransmission_rate_limiter), |
127 overhead_observer_(overhead_observer), | 125 overhead_observer_(overhead_observer), |
128 send_side_bwe_with_overhead_( | 126 send_side_bwe_with_overhead_( |
129 webrtc::field_trial::FindFullName( | 127 webrtc::field_trial::FindFullName( |
130 "WebRTC-SendSideBwe-WithOverhead") == "Enabled") { | 128 "WebRTC-SendSideBwe-WithOverhead") == "Enabled") { |
131 ssrc_ = ssrc_db_->CreateSSRC(); | |
132 RTC_DCHECK(ssrc_ != 0); | |
133 ssrc_rtx_ = ssrc_db_->CreateSSRC(); | |
134 RTC_DCHECK(ssrc_rtx_ != 0); | |
135 | |
136 // This random initialization is not intended to be cryptographic strong. | 129 // This random initialization is not intended to be cryptographic strong. |
137 timestamp_offset_ = random_.Rand<uint32_t>(); | 130 timestamp_offset_ = random_.Rand<uint32_t>(); |
138 // Random start, 16 bits. Can't be 0. | 131 // Random start, 16 bits. Can't be 0. |
139 sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 132 sequence_number_rtx_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
140 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 133 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
141 | 134 |
142 // Store FlexFEC packets in the packet history data structure, so they can | 135 // Store FlexFEC packets in the packet history data structure, so they can |
143 // be found when paced. | 136 // be found when paced. |
144 if (flexfec_sender) { | 137 if (flexfec_sender) { |
145 flexfec_packet_history_.SetStorePacketsStatus( | 138 flexfec_packet_history_.SetStorePacketsStatus( |
146 true, kMinFlexfecPacketsToStoreForPacing); | 139 true, kMinFlexfecPacketsToStoreForPacing); |
147 } | 140 } |
148 } | 141 } |
149 | 142 |
150 RTPSender::~RTPSender() { | 143 RTPSender::~RTPSender() { |
151 // TODO(tommi): Use a thread checker to ensure the object is created and | 144 // TODO(tommi): Use a thread checker to ensure the object is created and |
152 // deleted on the same thread. At the moment this isn't possible due to | 145 // deleted on the same thread. At the moment this isn't possible due to |
153 // voe::ChannelOwner in voice engine. To reproduce, run: | 146 // voe::ChannelOwner in voice engine. To reproduce, run: |
154 // voe_auto_test --automated --gtest_filter=*MixManyChannelsForStressOpus | 147 // voe_auto_test --automated --gtest_filter=*MixManyChannelsForStressOpus |
155 | 148 |
156 // TODO(tommi,holmer): We don't grab locks in the dtor before accessing member | 149 // TODO(tommi,holmer): We don't grab locks in the dtor before accessing member |
157 // variables but we grab them in all other methods. (what's the design?) | 150 // variables but we grab them in all other methods. (what's the design?) |
158 // Start documenting what thread we're on in what method so that it's easier | 151 // Start documenting what thread we're on in what method so that it's easier |
159 // to understand performance attributes and possibly remove locks. | 152 // to understand performance attributes and possibly remove locks. |
160 if (remote_ssrc_ != 0) { | |
161 ssrc_db_->ReturnSSRC(remote_ssrc_); | |
162 } | |
163 ssrc_db_->ReturnSSRC(ssrc_); | |
164 | |
165 SSRCDatabase::ReturnSSRCDatabase(); | |
166 while (!payload_type_map_.empty()) { | 153 while (!payload_type_map_.empty()) { |
167 std::map<int8_t, RtpUtility::Payload*>::iterator it = | 154 std::map<int8_t, RtpUtility::Payload*>::iterator it = |
168 payload_type_map_.begin(); | 155 payload_type_map_.begin(); |
169 delete it->second; | 156 delete it->second; |
170 payload_type_map_.erase(it); | 157 payload_type_map_.erase(it); |
171 } | 158 } |
172 } | 159 } |
173 | 160 |
174 uint16_t RTPSender::ActualSendBitrateKbit() const { | 161 uint16_t RTPSender::ActualSendBitrateKbit() const { |
175 rtc::CritScope cs(&statistics_crit_); | 162 rtc::CritScope cs(&statistics_crit_); |
(...skipping 27 matching lines...) Expand all Loading... |
203 switch (type) { | 190 switch (type) { |
204 case kRtpExtensionVideoRotation: | 191 case kRtpExtensionVideoRotation: |
205 case kRtpExtensionPlayoutDelay: | 192 case kRtpExtensionPlayoutDelay: |
206 case kRtpExtensionTransmissionTimeOffset: | 193 case kRtpExtensionTransmissionTimeOffset: |
207 case kRtpExtensionAbsoluteSendTime: | 194 case kRtpExtensionAbsoluteSendTime: |
208 case kRtpExtensionAudioLevel: | 195 case kRtpExtensionAudioLevel: |
209 case kRtpExtensionTransportSequenceNumber: | 196 case kRtpExtensionTransportSequenceNumber: |
210 return rtp_header_extension_map_.Register(type, id); | 197 return rtp_header_extension_map_.Register(type, id); |
211 case kRtpExtensionNone: | 198 case kRtpExtensionNone: |
212 case kRtpExtensionNumberOfExtensions: | 199 case kRtpExtensionNumberOfExtensions: |
213 LOG(LS_ERROR) << "Invalid RTP extension type for registration"; | 200 LOG(LS_ERROR) << "Invalid RTP extension type for registration."; |
214 return -1; | 201 return -1; |
215 } | 202 } |
216 return -1; | 203 return -1; |
217 } | 204 } |
218 | 205 |
219 bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) const { | 206 bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) const { |
220 rtc::CritScope lock(&send_critsect_); | 207 rtc::CritScope lock(&send_critsect_); |
221 return rtp_header_extension_map_.IsRegistered(type); | 208 return rtp_header_extension_map_.IsRegistered(type); |
222 } | 209 } |
223 | 210 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 rtx_ = mode; | 314 rtx_ = mode; |
328 } | 315 } |
329 | 316 |
330 int RTPSender::RtxStatus() const { | 317 int RTPSender::RtxStatus() const { |
331 rtc::CritScope lock(&send_critsect_); | 318 rtc::CritScope lock(&send_critsect_); |
332 return rtx_; | 319 return rtx_; |
333 } | 320 } |
334 | 321 |
335 void RTPSender::SetRtxSsrc(uint32_t ssrc) { | 322 void RTPSender::SetRtxSsrc(uint32_t ssrc) { |
336 rtc::CritScope lock(&send_critsect_); | 323 rtc::CritScope lock(&send_critsect_); |
337 ssrc_rtx_ = ssrc; | 324 ssrc_rtx_.emplace(ssrc); |
338 } | 325 } |
339 | 326 |
340 uint32_t RTPSender::RtxSsrc() const { | 327 uint32_t RTPSender::RtxSsrc() const { |
341 rtc::CritScope lock(&send_critsect_); | 328 rtc::CritScope lock(&send_critsect_); |
342 return ssrc_rtx_; | 329 RTC_DCHECK(ssrc_rtx_); |
| 330 return *ssrc_rtx_; |
343 } | 331 } |
344 | 332 |
345 void RTPSender::SetRtxPayloadType(int payload_type, | 333 void RTPSender::SetRtxPayloadType(int payload_type, |
346 int associated_payload_type) { | 334 int associated_payload_type) { |
347 rtc::CritScope lock(&send_critsect_); | 335 rtc::CritScope lock(&send_critsect_); |
348 RTC_DCHECK_LE(payload_type, 127); | 336 RTC_DCHECK_LE(payload_type, 127); |
349 RTC_DCHECK_LE(associated_payload_type, 127); | 337 RTC_DCHECK_LE(associated_payload_type, 127); |
350 if (payload_type < 0) { | 338 if (payload_type < 0) { |
351 LOG(LS_ERROR) << "Invalid RTX payload type: " << payload_type; | 339 LOG(LS_ERROR) << "Invalid RTX payload type: " << payload_type << "."; |
352 return; | 340 return; |
353 } | 341 } |
354 | 342 |
355 rtx_payload_type_map_[associated_payload_type] = payload_type; | 343 rtx_payload_type_map_[associated_payload_type] = payload_type; |
356 } | 344 } |
357 | 345 |
358 int32_t RTPSender::CheckPayloadType(int8_t payload_type, | 346 int32_t RTPSender::CheckPayloadType(int8_t payload_type, |
359 RtpVideoCodecTypes* video_type) { | 347 RtpVideoCodecTypes* video_type) { |
360 rtc::CritScope lock(&send_critsect_); | 348 rtc::CritScope lock(&send_critsect_); |
361 | 349 |
362 if (payload_type < 0) { | 350 if (payload_type < 0) { |
363 LOG(LS_ERROR) << "Invalid payload_type " << payload_type; | 351 LOG(LS_ERROR) << "Invalid payload_type " << payload_type << "."; |
364 return -1; | 352 return -1; |
365 } | 353 } |
366 if (payload_type_ == payload_type) { | 354 if (payload_type_ == payload_type) { |
367 if (!audio_configured_) { | 355 if (!audio_configured_) { |
368 *video_type = video_->VideoCodecType(); | 356 *video_type = video_->VideoCodecType(); |
369 } | 357 } |
370 return 0; | 358 return 0; |
371 } | 359 } |
372 std::map<int8_t, RtpUtility::Payload*>::iterator it = | 360 std::map<int8_t, RtpUtility::Payload*>::iterator it = |
373 payload_type_map_.find(payload_type); | 361 payload_type_map_.find(payload_type); |
(...skipping 20 matching lines...) Expand all Loading... |
394 size_t payload_size, | 382 size_t payload_size, |
395 const RTPFragmentationHeader* fragmentation, | 383 const RTPFragmentationHeader* fragmentation, |
396 const RTPVideoHeader* rtp_header, | 384 const RTPVideoHeader* rtp_header, |
397 uint32_t* transport_frame_id_out) { | 385 uint32_t* transport_frame_id_out) { |
398 uint32_t ssrc; | 386 uint32_t ssrc; |
399 uint16_t sequence_number; | 387 uint16_t sequence_number; |
400 uint32_t rtp_timestamp; | 388 uint32_t rtp_timestamp; |
401 { | 389 { |
402 // Drop this packet if we're not sending media packets. | 390 // Drop this packet if we're not sending media packets. |
403 rtc::CritScope lock(&send_critsect_); | 391 rtc::CritScope lock(&send_critsect_); |
404 ssrc = ssrc_; | 392 RTC_DCHECK(ssrc_); |
| 393 |
| 394 ssrc = *ssrc_; |
405 sequence_number = sequence_number_; | 395 sequence_number = sequence_number_; |
406 rtp_timestamp = timestamp_offset_ + capture_timestamp; | 396 rtp_timestamp = timestamp_offset_ + capture_timestamp; |
407 if (transport_frame_id_out) | 397 if (transport_frame_id_out) |
408 *transport_frame_id_out = rtp_timestamp; | 398 *transport_frame_id_out = rtp_timestamp; |
409 if (!sending_media_) | 399 if (!sending_media_) |
410 return true; | 400 return true; |
411 } | 401 } |
412 RtpVideoCodecTypes video_type = kRtpVideoGeneric; | 402 RtpVideoCodecTypes video_type = kRtpVideoGeneric; |
413 if (CheckPayloadType(payload_type, &video_type) != 0) { | 403 if (CheckPayloadType(payload_type, &video_type) != 0) { |
414 LOG(LS_ERROR) << "Don't send data with unknown payload type: " | 404 LOG(LS_ERROR) << "Don't send data with unknown payload type: " |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 if (rtx_ == kRtxOff) { | 504 if (rtx_ == kRtxOff) { |
515 if (payload_type_ == -1) | 505 if (payload_type_ == -1) |
516 break; | 506 break; |
517 // Without RTX we can't send padding in the middle of frames. | 507 // Without RTX we can't send padding in the middle of frames. |
518 // For audio marker bits doesn't mark the end of a frame and frames | 508 // For audio marker bits doesn't mark the end of a frame and frames |
519 // are usually a single packet, so for now we don't apply this rule | 509 // are usually a single packet, so for now we don't apply this rule |
520 // for audio. | 510 // for audio. |
521 if (!audio_configured_ && !last_packet_marker_bit_) { | 511 if (!audio_configured_ && !last_packet_marker_bit_) { |
522 break; | 512 break; |
523 } | 513 } |
524 ssrc = ssrc_; | 514 if (!ssrc_) { |
| 515 LOG(LS_ERROR) << "SSRC unset."; |
| 516 return 0; |
| 517 } |
| 518 |
| 519 RTC_DCHECK(ssrc_); |
| 520 ssrc = *ssrc_; |
| 521 |
525 sequence_number = sequence_number_; | 522 sequence_number = sequence_number_; |
526 ++sequence_number_; | 523 ++sequence_number_; |
527 payload_type = payload_type_; | 524 payload_type = payload_type_; |
528 over_rtx = false; | 525 over_rtx = false; |
529 } else { | 526 } else { |
530 // Without abs-send-time or transport sequence number a media packet | 527 // Without abs-send-time or transport sequence number a media packet |
531 // must be sent before padding so that the timestamps used for | 528 // must be sent before padding so that the timestamps used for |
532 // estimation are correct. | 529 // estimation are correct. |
533 if (!media_has_been_sent_ && | 530 if (!media_has_been_sent_ && |
534 !(rtp_header_extension_map_.IsRegistered(AbsoluteSendTime::kId) || | 531 !(rtp_header_extension_map_.IsRegistered(AbsoluteSendTime::kId) || |
535 (rtp_header_extension_map_.IsRegistered( | 532 (rtp_header_extension_map_.IsRegistered( |
536 TransportSequenceNumber::kId) && | 533 TransportSequenceNumber::kId) && |
537 transport_sequence_number_allocator_))) { | 534 transport_sequence_number_allocator_))) { |
538 break; | 535 break; |
539 } | 536 } |
540 // Only change change the timestamp of padding packets sent over RTX. | 537 // Only change change the timestamp of padding packets sent over RTX. |
541 // Padding only packets over RTP has to be sent as part of a media | 538 // Padding only packets over RTP has to be sent as part of a media |
542 // frame (and therefore the same timestamp). | 539 // frame (and therefore the same timestamp). |
543 if (last_timestamp_time_ms_ > 0) { | 540 if (last_timestamp_time_ms_ > 0) { |
544 timestamp += | 541 timestamp += |
545 (now_ms - last_timestamp_time_ms_) * kTimestampTicksPerMs; | 542 (now_ms - last_timestamp_time_ms_) * kTimestampTicksPerMs; |
546 capture_time_ms += (now_ms - last_timestamp_time_ms_); | 543 capture_time_ms += (now_ms - last_timestamp_time_ms_); |
547 } | 544 } |
548 ssrc = ssrc_rtx_; | 545 if (!ssrc_rtx_) { |
| 546 LOG(LS_ERROR) << "RTX SSRC unset."; |
| 547 return 0; |
| 548 } |
| 549 RTC_DCHECK(ssrc_rtx_); |
| 550 ssrc = *ssrc_rtx_; |
549 sequence_number = sequence_number_rtx_; | 551 sequence_number = sequence_number_rtx_; |
550 ++sequence_number_rtx_; | 552 ++sequence_number_rtx_; |
551 payload_type = rtx_payload_type_map_.begin()->second; | 553 payload_type = rtx_payload_type_map_.begin()->second; |
552 over_rtx = true; | 554 over_rtx = true; |
553 } | 555 } |
554 } | 556 } |
555 | 557 |
556 RtpPacketToSend padding_packet(&rtp_header_extension_map_); | 558 RtpPacketToSend padding_packet(&rtp_header_extension_map_); |
557 padding_packet.SetPayloadType(payload_type); | 559 padding_packet.SetPayloadType(payload_type); |
558 padding_packet.SetMarker(false); | 560 padding_packet.SetMarker(false); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 if (event_log_ && bytes_sent > 0) { | 640 if (event_log_ && bytes_sent > 0) { |
639 event_log_->LogRtpHeader(kOutgoingPacket, MediaType::ANY, packet.data(), | 641 event_log_->LogRtpHeader(kOutgoingPacket, MediaType::ANY, packet.data(), |
640 packet.size()); | 642 packet.size()); |
641 } | 643 } |
642 } | 644 } |
643 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), | 645 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), |
644 "RTPSender::SendPacketToNetwork", "size", packet.size(), | 646 "RTPSender::SendPacketToNetwork", "size", packet.size(), |
645 "sent", bytes_sent); | 647 "sent", bytes_sent); |
646 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer. | 648 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer. |
647 if (bytes_sent <= 0) { | 649 if (bytes_sent <= 0) { |
648 LOG(LS_WARNING) << "Transport failed to send packet"; | 650 LOG(LS_WARNING) << "Transport failed to send packet."; |
649 return false; | 651 return false; |
650 } | 652 } |
651 return true; | 653 return true; |
652 } | 654 } |
653 | 655 |
654 int RTPSender::SelectiveRetransmissions() const { | 656 int RTPSender::SelectiveRetransmissions() const { |
655 if (!video_) | 657 if (!video_) |
656 return -1; | 658 return -1; |
657 return video_->SelectiveRetransmissions(); | 659 return video_->SelectiveRetransmissions(); |
658 } | 660 } |
659 | 661 |
660 int RTPSender::SetSelectiveRetransmissions(uint8_t settings) { | 662 int RTPSender::SetSelectiveRetransmissions(uint8_t settings) { |
661 if (!video_) | 663 if (!video_) |
662 return -1; | 664 return -1; |
663 video_->SetSelectiveRetransmissions(settings); | 665 video_->SetSelectiveRetransmissions(settings); |
664 return 0; | 666 return 0; |
665 } | 667 } |
666 | 668 |
667 void RTPSender::OnReceivedNack( | 669 void RTPSender::OnReceivedNack( |
668 const std::vector<uint16_t>& nack_sequence_numbers, | 670 const std::vector<uint16_t>& nack_sequence_numbers, |
669 int64_t avg_rtt) { | 671 int64_t avg_rtt) { |
670 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), | 672 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), |
671 "RTPSender::OnReceivedNACK", "num_seqnum", | 673 "RTPSender::OnReceivedNACK", "num_seqnum", |
672 nack_sequence_numbers.size(), "avg_rtt", avg_rtt); | 674 nack_sequence_numbers.size(), "avg_rtt", avg_rtt); |
673 for (uint16_t seq_no : nack_sequence_numbers) { | 675 for (uint16_t seq_no : nack_sequence_numbers) { |
674 const int32_t bytes_sent = ReSendPacket(seq_no, 5 + avg_rtt); | 676 const int32_t bytes_sent = ReSendPacket(seq_no, 5 + avg_rtt); |
675 if (bytes_sent < 0) { | 677 if (bytes_sent < 0) { |
676 // Failed to send one Sequence number. Give up the rest in this nack. | 678 // Failed to send one Sequence number. Give up the rest in this nack. |
677 LOG(LS_WARNING) << "Failed resending RTP packet " << seq_no | 679 LOG(LS_WARNING) << "Failed resending RTP packet " << seq_no |
678 << ", Discard rest of packets"; | 680 << ", Discard rest of packets."; |
679 break; | 681 break; |
680 } | 682 } |
681 } | 683 } |
682 } | 684 } |
683 | 685 |
684 void RTPSender::OnReceivedRtcpReportBlocks( | 686 void RTPSender::OnReceivedRtcpReportBlocks( |
685 const ReportBlockList& report_blocks) { | 687 const ReportBlockList& report_blocks) { |
686 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks); | 688 playout_delay_oracle_.OnReceivedRtcpReportBlocks(report_blocks); |
687 } | 689 } |
688 | 690 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
912 | 914 |
913 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) { | 915 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) { |
914 if (!send_side_delay_observer_ || capture_time_ms <= 0) | 916 if (!send_side_delay_observer_ || capture_time_ms <= 0) |
915 return; | 917 return; |
916 | 918 |
917 uint32_t ssrc; | 919 uint32_t ssrc; |
918 int avg_delay_ms = 0; | 920 int avg_delay_ms = 0; |
919 int max_delay_ms = 0; | 921 int max_delay_ms = 0; |
920 { | 922 { |
921 rtc::CritScope lock(&send_critsect_); | 923 rtc::CritScope lock(&send_critsect_); |
922 ssrc = ssrc_; | 924 if (!ssrc_) |
| 925 return; |
| 926 ssrc = *ssrc_; |
923 } | 927 } |
924 { | 928 { |
925 rtc::CritScope cs(&statistics_crit_); | 929 rtc::CritScope cs(&statistics_crit_); |
926 // TODO(holmer): Compute this iteratively instead. | 930 // TODO(holmer): Compute this iteratively instead. |
927 send_delays_[now_ms] = now_ms - capture_time_ms; | 931 send_delays_[now_ms] = now_ms - capture_time_ms; |
928 send_delays_.erase(send_delays_.begin(), | 932 send_delays_.erase(send_delays_.begin(), |
929 send_delays_.lower_bound(now_ms - | 933 send_delays_.lower_bound(now_ms - |
930 kSendSideDelayWindowMs)); | 934 kSendSideDelayWindowMs)); |
931 int num_delays = 0; | 935 int num_delays = 0; |
932 for (auto it = send_delays_.upper_bound(now_ms - kSendSideDelayWindowMs); | 936 for (auto it = send_delays_.upper_bound(now_ms - kSendSideDelayWindowMs); |
(...skipping 19 matching lines...) Expand all Loading... |
952 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc); | 956 send_packet_observer_->OnSendPacket(packet_id, capture_time_ms, ssrc); |
953 } | 957 } |
954 | 958 |
955 void RTPSender::ProcessBitrate() { | 959 void RTPSender::ProcessBitrate() { |
956 if (!bitrate_callback_) | 960 if (!bitrate_callback_) |
957 return; | 961 return; |
958 int64_t now_ms = clock_->TimeInMilliseconds(); | 962 int64_t now_ms = clock_->TimeInMilliseconds(); |
959 uint32_t ssrc; | 963 uint32_t ssrc; |
960 { | 964 { |
961 rtc::CritScope lock(&send_critsect_); | 965 rtc::CritScope lock(&send_critsect_); |
962 ssrc = ssrc_; | 966 if (!ssrc_) |
| 967 return; |
| 968 ssrc = *ssrc_; |
963 } | 969 } |
964 | 970 |
965 rtc::CritScope lock(&statistics_crit_); | 971 rtc::CritScope lock(&statistics_crit_); |
966 bitrate_callback_->Notify(total_bitrate_sent_.Rate(now_ms).value_or(0), | 972 bitrate_callback_->Notify(total_bitrate_sent_.Rate(now_ms).value_or(0), |
967 nack_bitrate_sent_.Rate(now_ms).value_or(0), ssrc); | 973 nack_bitrate_sent_.Rate(now_ms).value_or(0), ssrc); |
968 } | 974 } |
969 | 975 |
970 size_t RTPSender::RtpHeaderLength() const { | 976 size_t RTPSender::RtpHeaderLength() const { |
971 rtc::CritScope lock(&send_critsect_); | 977 rtc::CritScope lock(&send_critsect_); |
972 size_t rtp_header_length = kRtpHeaderLength; | 978 size_t rtp_header_length = kRtpHeaderLength; |
(...skipping 13 matching lines...) Expand all Loading... |
986 StreamDataCounters* rtx_stats) const { | 992 StreamDataCounters* rtx_stats) const { |
987 rtc::CritScope lock(&statistics_crit_); | 993 rtc::CritScope lock(&statistics_crit_); |
988 *rtp_stats = rtp_stats_; | 994 *rtp_stats = rtp_stats_; |
989 *rtx_stats = rtx_rtp_stats_; | 995 *rtx_stats = rtx_rtp_stats_; |
990 } | 996 } |
991 | 997 |
992 std::unique_ptr<RtpPacketToSend> RTPSender::AllocatePacket() const { | 998 std::unique_ptr<RtpPacketToSend> RTPSender::AllocatePacket() const { |
993 rtc::CritScope lock(&send_critsect_); | 999 rtc::CritScope lock(&send_critsect_); |
994 std::unique_ptr<RtpPacketToSend> packet( | 1000 std::unique_ptr<RtpPacketToSend> packet( |
995 new RtpPacketToSend(&rtp_header_extension_map_, max_packet_size_)); | 1001 new RtpPacketToSend(&rtp_header_extension_map_, max_packet_size_)); |
996 packet->SetSsrc(ssrc_); | 1002 RTC_DCHECK(ssrc_); |
| 1003 packet->SetSsrc(*ssrc_); |
997 packet->SetCsrcs(csrcs_); | 1004 packet->SetCsrcs(csrcs_); |
998 // Reserve extensions, if registered, RtpSender set in SendToNetwork. | 1005 // Reserve extensions, if registered, RtpSender set in SendToNetwork. |
999 packet->ReserveExtension<AbsoluteSendTime>(); | 1006 packet->ReserveExtension<AbsoluteSendTime>(); |
1000 packet->ReserveExtension<TransmissionOffset>(); | 1007 packet->ReserveExtension<TransmissionOffset>(); |
1001 packet->ReserveExtension<TransportSequenceNumber>(); | 1008 packet->ReserveExtension<TransportSequenceNumber>(); |
1002 if (playout_delay_oracle_.send_playout_delay()) { | 1009 if (playout_delay_oracle_.send_playout_delay()) { |
1003 packet->SetExtension<PlayoutDelayLimits>( | 1010 packet->SetExtension<PlayoutDelayLimits>( |
1004 playout_delay_oracle_.playout_delay()); | 1011 playout_delay_oracle_.playout_delay()); |
1005 } | 1012 } |
1006 return packet; | 1013 return packet; |
1007 } | 1014 } |
1008 | 1015 |
1009 bool RTPSender::AssignSequenceNumber(RtpPacketToSend* packet) { | 1016 bool RTPSender::AssignSequenceNumber(RtpPacketToSend* packet) { |
1010 rtc::CritScope lock(&send_critsect_); | 1017 rtc::CritScope lock(&send_critsect_); |
1011 if (!sending_media_) | 1018 if (!sending_media_) |
1012 return false; | 1019 return false; |
1013 RTC_DCHECK_EQ(packet->Ssrc(), ssrc_); | 1020 RTC_DCHECK(packet->Ssrc() == ssrc_); |
1014 packet->SetSequenceNumber(sequence_number_++); | 1021 packet->SetSequenceNumber(sequence_number_++); |
1015 | 1022 |
1016 // Remember marker bit to determine if padding can be inserted with | 1023 // Remember marker bit to determine if padding can be inserted with |
1017 // sequence number following |packet|. | 1024 // sequence number following |packet|. |
1018 last_packet_marker_bit_ = packet->Marker(); | 1025 last_packet_marker_bit_ = packet->Marker(); |
1019 // Save timestamps to generate timestamp field and extensions for the padding. | 1026 // Save timestamps to generate timestamp field and extensions for the padding. |
1020 last_rtp_timestamp_ = packet->Timestamp(); | 1027 last_rtp_timestamp_ = packet->Timestamp(); |
1021 last_timestamp_time_ms_ = clock_->TimeInMilliseconds(); | 1028 last_timestamp_time_ms_ = clock_->TimeInMilliseconds(); |
1022 capture_time_ms_ = packet->capture_time_ms(); | 1029 capture_time_ms_ = packet->capture_time_ms(); |
1023 return true; | 1030 return true; |
(...skipping 11 matching lines...) Expand all Loading... |
1035 return false; | 1042 return false; |
1036 | 1043 |
1037 *packet_id = transport_sequence_number_allocator_->AllocateSequenceNumber(); | 1044 *packet_id = transport_sequence_number_allocator_->AllocateSequenceNumber(); |
1038 | 1045 |
1039 if (!packet->SetExtension<TransportSequenceNumber>(*packet_id)) | 1046 if (!packet->SetExtension<TransportSequenceNumber>(*packet_id)) |
1040 return false; | 1047 return false; |
1041 | 1048 |
1042 return true; | 1049 return true; |
1043 } | 1050 } |
1044 | 1051 |
1045 void RTPSender::SetSendingStatus(bool enabled) { | |
1046 if (!enabled) { | |
1047 rtc::CritScope lock(&send_critsect_); | |
1048 if (!ssrc_forced_) { | |
1049 // Generate a new SSRC. | |
1050 ssrc_db_->ReturnSSRC(ssrc_); | |
1051 ssrc_ = ssrc_db_->CreateSSRC(); | |
1052 RTC_DCHECK(ssrc_ != 0); | |
1053 } | |
1054 // Don't initialize seq number if SSRC passed externally. | |
1055 if (!sequence_number_forced_ && !ssrc_forced_) { | |
1056 // Generate a new sequence number. | |
1057 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | |
1058 } | |
1059 } | |
1060 } | |
1061 | |
1062 void RTPSender::SetSendingMediaStatus(bool enabled) { | 1052 void RTPSender::SetSendingMediaStatus(bool enabled) { |
1063 rtc::CritScope lock(&send_critsect_); | 1053 rtc::CritScope lock(&send_critsect_); |
1064 sending_media_ = enabled; | 1054 sending_media_ = enabled; |
1065 } | 1055 } |
1066 | 1056 |
1067 bool RTPSender::SendingMedia() const { | 1057 bool RTPSender::SendingMedia() const { |
1068 rtc::CritScope lock(&send_critsect_); | 1058 rtc::CritScope lock(&send_critsect_); |
1069 return sending_media_; | 1059 return sending_media_; |
1070 } | 1060 } |
1071 | 1061 |
1072 void RTPSender::SetTimestampOffset(uint32_t timestamp) { | 1062 void RTPSender::SetTimestampOffset(uint32_t timestamp) { |
1073 rtc::CritScope lock(&send_critsect_); | 1063 rtc::CritScope lock(&send_critsect_); |
1074 timestamp_offset_ = timestamp; | 1064 timestamp_offset_ = timestamp; |
1075 } | 1065 } |
1076 | 1066 |
1077 uint32_t RTPSender::TimestampOffset() const { | 1067 uint32_t RTPSender::TimestampOffset() const { |
1078 rtc::CritScope lock(&send_critsect_); | 1068 rtc::CritScope lock(&send_critsect_); |
1079 return timestamp_offset_; | 1069 return timestamp_offset_; |
1080 } | 1070 } |
1081 | 1071 |
1082 uint32_t RTPSender::GenerateNewSSRC() { | |
1083 // If configured via API, return 0. | |
1084 rtc::CritScope lock(&send_critsect_); | |
1085 | |
1086 if (ssrc_forced_) { | |
1087 return 0; | |
1088 } | |
1089 ssrc_ = ssrc_db_->CreateSSRC(); | |
1090 RTC_DCHECK(ssrc_ != 0); | |
1091 return ssrc_; | |
1092 } | |
1093 | |
1094 void RTPSender::SetSSRC(uint32_t ssrc) { | 1072 void RTPSender::SetSSRC(uint32_t ssrc) { |
1095 // This is configured via the API. | 1073 // This is configured via the API. |
1096 rtc::CritScope lock(&send_critsect_); | 1074 rtc::CritScope lock(&send_critsect_); |
1097 | 1075 |
1098 if (ssrc_ == ssrc && ssrc_forced_) { | 1076 if (ssrc_ == ssrc) { |
1099 return; // Since it's same ssrc, don't reset anything. | 1077 return; // Since it's same ssrc, don't reset anything. |
1100 } | 1078 } |
1101 ssrc_forced_ = true; | 1079 ssrc_.emplace(ssrc); |
1102 ssrc_db_->ReturnSSRC(ssrc_); | |
1103 ssrc_db_->RegisterSSRC(ssrc); | |
1104 ssrc_ = ssrc; | |
1105 if (!sequence_number_forced_) { | 1080 if (!sequence_number_forced_) { |
1106 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); | 1081 sequence_number_ = random_.Rand(1, kMaxInitRtpSeqNumber); |
1107 } | 1082 } |
1108 } | 1083 } |
1109 | 1084 |
1110 uint32_t RTPSender::SSRC() const { | 1085 uint32_t RTPSender::SSRC() const { |
1111 rtc::CritScope lock(&send_critsect_); | 1086 rtc::CritScope lock(&send_critsect_); |
1112 return ssrc_; | 1087 RTC_DCHECK(ssrc_); |
| 1088 return *ssrc_; |
1113 } | 1089 } |
1114 | 1090 |
1115 rtc::Optional<uint32_t> RTPSender::FlexfecSsrc() const { | 1091 rtc::Optional<uint32_t> RTPSender::FlexfecSsrc() const { |
1116 if (video_) { | 1092 if (video_) { |
1117 return video_->FlexfecSsrc(); | 1093 return video_->FlexfecSsrc(); |
1118 } | 1094 } |
1119 return rtc::Optional<uint32_t>(); | 1095 return rtc::Optional<uint32_t>(); |
1120 } | 1096 } |
1121 | 1097 |
1122 void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) { | 1098 void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1182 // when transport interface would be updated to take buffer class. | 1158 // when transport interface would be updated to take buffer class. |
1183 std::unique_ptr<RtpPacketToSend> rtx_packet(new RtpPacketToSend( | 1159 std::unique_ptr<RtpPacketToSend> rtx_packet(new RtpPacketToSend( |
1184 &rtp_header_extension_map_, packet.size() + kRtxHeaderSize)); | 1160 &rtp_header_extension_map_, packet.size() + kRtxHeaderSize)); |
1185 // Add original RTP header. | 1161 // Add original RTP header. |
1186 rtx_packet->CopyHeaderFrom(packet); | 1162 rtx_packet->CopyHeaderFrom(packet); |
1187 { | 1163 { |
1188 rtc::CritScope lock(&send_critsect_); | 1164 rtc::CritScope lock(&send_critsect_); |
1189 if (!sending_media_) | 1165 if (!sending_media_) |
1190 return nullptr; | 1166 return nullptr; |
1191 | 1167 |
| 1168 RTC_DCHECK(ssrc_rtx_); |
| 1169 |
1192 // Replace payload type. | 1170 // Replace payload type. |
1193 auto kv = rtx_payload_type_map_.find(packet.PayloadType()); | 1171 auto kv = rtx_payload_type_map_.find(packet.PayloadType()); |
1194 if (kv == rtx_payload_type_map_.end()) | 1172 if (kv == rtx_payload_type_map_.end()) |
1195 return nullptr; | 1173 return nullptr; |
1196 rtx_packet->SetPayloadType(kv->second); | 1174 rtx_packet->SetPayloadType(kv->second); |
1197 | 1175 |
1198 // Replace sequence number. | 1176 // Replace sequence number. |
1199 rtx_packet->SetSequenceNumber(sequence_number_rtx_++); | 1177 rtx_packet->SetSequenceNumber(sequence_number_rtx_++); |
1200 | 1178 |
1201 // Replace SSRC. | 1179 // Replace SSRC. |
1202 rtx_packet->SetSsrc(ssrc_rtx_); | 1180 rtx_packet->SetSsrc(*ssrc_rtx_); |
1203 } | 1181 } |
1204 | 1182 |
1205 uint8_t* rtx_payload = | 1183 uint8_t* rtx_payload = |
1206 rtx_packet->AllocatePayload(packet.payload_size() + kRtxHeaderSize); | 1184 rtx_packet->AllocatePayload(packet.payload_size() + kRtxHeaderSize); |
1207 RTC_DCHECK(rtx_payload); | 1185 RTC_DCHECK(rtx_payload); |
1208 // Add OSN (original sequence number). | 1186 // Add OSN (original sequence number). |
1209 ByteWriter<uint16_t>::WriteBigEndian(rtx_payload, packet.SequenceNumber()); | 1187 ByteWriter<uint16_t>::WriteBigEndian(rtx_payload, packet.SequenceNumber()); |
1210 | 1188 |
1211 // Add original payload data. | 1189 // Add original payload data. |
1212 auto payload = packet.payload(); | 1190 auto payload = packet.payload(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1294 if (rtp_overhead_bytes_per_packet_ == packet.headers_size()) { | 1272 if (rtp_overhead_bytes_per_packet_ == packet.headers_size()) { |
1295 return; | 1273 return; |
1296 } | 1274 } |
1297 rtp_overhead_bytes_per_packet_ = packet.headers_size(); | 1275 rtp_overhead_bytes_per_packet_ = packet.headers_size(); |
1298 overhead_bytes_per_packet = rtp_overhead_bytes_per_packet_; | 1276 overhead_bytes_per_packet = rtp_overhead_bytes_per_packet_; |
1299 } | 1277 } |
1300 overhead_observer_->OnOverheadChanged(overhead_bytes_per_packet); | 1278 overhead_observer_->OnOverheadChanged(overhead_bytes_per_packet); |
1301 } | 1279 } |
1302 | 1280 |
1303 } // namespace webrtc | 1281 } // namespace webrtc |
OLD | NEW |