2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
13 #include <stdlib.h> // srand
15 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
16 #include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18 #include "webrtc/system_wrappers/interface/logging.h"
19 #include "webrtc/system_wrappers/interface/tick_util.h"
20 #include "webrtc/system_wrappers/interface/trace_event.h"
24 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25 const int kMaxPaddingLength = 224;
26 const int kSendSideDelayWindowMs = 1000;
30 const char* FrameTypeToString(const FrameType frame_type) {
32 case kFrameEmpty: return "empty";
33 case kAudioFrameSpeech: return "audio_speech";
34 case kAudioFrameCN: return "audio_cn";
35 case kVideoFrameKey: return "video_key";
36 case kVideoFrameDelta: return "video_delta";
43 class BitrateAggregator {
45 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
46 : callback_(bitrate_callback),
47 total_bitrate_observer_(*this),
48 retransmit_bitrate_observer_(*this),
51 void OnStatsUpdated() const {
53 callback_->Notify(total_bitrate_observer_.statistics(),
54 retransmit_bitrate_observer_.statistics(),
58 Bitrate::Observer* total_bitrate_observer() {
59 return &total_bitrate_observer_;
61 Bitrate::Observer* retransmit_bitrate_observer() {
62 return &retransmit_bitrate_observer_;
65 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
68 // We assume that these observers are called on the same thread, which is
69 // true for RtpSender as they are called on the Process thread.
70 class BitrateObserver : public Bitrate::Observer {
72 explicit BitrateObserver(const BitrateAggregator& aggregator)
73 : aggregator_(aggregator) {}
75 // Implements Bitrate::Observer.
76 virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE {
78 aggregator_.OnStatsUpdated();
81 BitrateStatistics statistics() const { return statistics_; }
84 BitrateStatistics statistics_;
85 const BitrateAggregator& aggregator_;
88 BitrateStatisticsObserver* const callback_;
89 BitrateObserver total_bitrate_observer_;
90 BitrateObserver retransmit_bitrate_observer_;
94 RTPSender::RTPSender(const int32_t id,
98 RtpAudioFeedback* audio_feedback,
99 PacedSender* paced_sender,
100 BitrateStatisticsObserver* bitrate_callback,
101 FrameCountObserver* frame_count_observer,
102 SendSideDelayObserver* send_side_delay_observer)
104 // TODO(holmer): Remove this conversion when we remove the use of
106 clock_delta_ms_(clock_->TimeInMilliseconds() -
107 TickTime::MillisecondTimestamp()),
108 bitrates_(new BitrateAggregator(bitrate_callback)),
109 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
111 audio_configured_(audio),
114 paced_sender_(paced_sender),
115 last_capture_time_ms_sent_(0),
116 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
117 transport_(transport),
118 sending_media_(true), // Default to sending media.
119 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
120 packet_over_head_(28),
123 rtp_header_extension_map_(),
124 transmission_time_offset_(0),
125 absolute_send_time_(0),
127 nack_byte_count_times_(),
129 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
130 packet_history_(clock),
132 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
133 rtp_stats_callback_(NULL),
134 frame_count_observer_(frame_count_observer),
135 send_side_delay_observer_(send_side_delay_observer),
137 start_timestamp_forced_(false),
139 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
141 sequence_number_forced_(false),
145 last_timestamp_time_ms_(0),
146 media_has_been_sent_(false),
147 last_packet_marker_bit_(false),
150 include_csrcs_(true),
152 payload_type_rtx_(-1),
153 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
155 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
156 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
157 memset(csrcs_, 0, sizeof(csrcs_));
158 // We need to seed the random generator.
159 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
160 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
161 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
162 bitrates_->set_ssrc(ssrc_);
163 // Random start, 16 bits. Can't be 0.
164 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
165 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
168 audio_ = new RTPSenderAudio(id, clock_, this);
169 audio_->RegisterAudioCallback(audio_feedback);
171 video_ = new RTPSenderVideo(clock_, this);
175 RTPSender::~RTPSender() {
176 if (remote_ssrc_ != 0) {
177 ssrc_db_.ReturnSSRC(remote_ssrc_);
179 ssrc_db_.ReturnSSRC(ssrc_);
181 SSRCDatabase::ReturnSSRCDatabase();
182 delete send_critsect_;
183 while (!payload_type_map_.empty()) {
184 std::map<int8_t, RtpUtility::Payload*>::iterator it =
185 payload_type_map_.begin();
187 payload_type_map_.erase(it);
193 void RTPSender::SetTargetBitrate(uint32_t bitrate) {
194 CriticalSectionScoped cs(target_bitrate_critsect_.get());
195 target_bitrate_ = bitrate;
198 uint32_t RTPSender::GetTargetBitrate() {
199 CriticalSectionScoped cs(target_bitrate_critsect_.get());
200 return target_bitrate_;
203 uint16_t RTPSender::ActualSendBitrateKbit() const {
204 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
207 uint32_t RTPSender::VideoBitrateSent() const {
209 return video_->VideoBitrateSent();
214 uint32_t RTPSender::FecOverheadRate() const {
216 return video_->FecOverheadRate();
221 uint32_t RTPSender::NackOverheadRate() const {
222 return nack_bitrate_.BitrateLast();
225 bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
226 int* max_send_delay_ms) const {
227 CriticalSectionScoped lock(statistics_crit_.get());
228 SendDelayMap::const_iterator it = send_delays_.upper_bound(
229 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
230 if (it == send_delays_.end())
233 for (; it != send_delays_.end(); ++it) {
234 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
235 *avg_send_delay_ms += it->second;
238 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
242 int32_t RTPSender::SetTransmissionTimeOffset(
243 const int32_t transmission_time_offset) {
244 if (transmission_time_offset > (0x800000 - 1) ||
245 transmission_time_offset < -(0x800000 - 1)) { // Word24.
248 CriticalSectionScoped cs(send_critsect_);
249 transmission_time_offset_ = transmission_time_offset;
253 int32_t RTPSender::SetAbsoluteSendTime(
254 const uint32_t absolute_send_time) {
255 if (absolute_send_time > 0xffffff) { // UWord24.
258 CriticalSectionScoped cs(send_critsect_);
259 absolute_send_time_ = absolute_send_time;
263 int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
265 CriticalSectionScoped cs(send_critsect_);
266 return rtp_header_extension_map_.Register(type, id);
269 int32_t RTPSender::DeregisterRtpHeaderExtension(
270 const RTPExtensionType type) {
271 CriticalSectionScoped cs(send_critsect_);
272 return rtp_header_extension_map_.Deregister(type);
275 uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
276 CriticalSectionScoped cs(send_critsect_);
277 return rtp_header_extension_map_.GetTotalLengthInBytes();
280 int32_t RTPSender::RegisterPayload(
281 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
282 const int8_t payload_number, const uint32_t frequency,
283 const uint8_t channels, const uint32_t rate) {
284 assert(payload_name);
285 CriticalSectionScoped cs(send_critsect_);
287 std::map<int8_t, RtpUtility::Payload*>::iterator it =
288 payload_type_map_.find(payload_number);
290 if (payload_type_map_.end() != it) {
291 // We already use this payload type.
292 RtpUtility::Payload* payload = it->second;
295 // Check if it's the same as we already have.
296 if (RtpUtility::StringCompare(
297 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
298 if (audio_configured_ && payload->audio &&
299 payload->typeSpecific.Audio.frequency == frequency &&
300 (payload->typeSpecific.Audio.rate == rate ||
301 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
302 payload->typeSpecific.Audio.rate = rate;
303 // Ensure that we update the rate if new or old is zero.
306 if (!audio_configured_ && !payload->audio) {
312 int32_t ret_val = -1;
313 RtpUtility::Payload* payload = NULL;
314 if (audio_configured_) {
315 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
316 frequency, channels, rate, payload);
318 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
322 payload_type_map_[payload_number] = payload;
327 int32_t RTPSender::DeRegisterSendPayload(
328 const int8_t payload_type) {
329 CriticalSectionScoped lock(send_critsect_);
331 std::map<int8_t, RtpUtility::Payload*>::iterator it =
332 payload_type_map_.find(payload_type);
334 if (payload_type_map_.end() == it) {
337 RtpUtility::Payload* payload = it->second;
339 payload_type_map_.erase(it);
343 void RTPSender::SetSendPayloadType(int8_t payload_type) {
344 CriticalSectionScoped cs(send_critsect_);
345 payload_type_ = payload_type;
348 int8_t RTPSender::SendPayloadType() const {
349 CriticalSectionScoped cs(send_critsect_);
350 return payload_type_;
353 int RTPSender::SendPayloadFrequency() const {
354 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
357 int32_t RTPSender::SetMaxPayloadLength(
358 const uint16_t max_payload_length,
359 const uint16_t packet_over_head) {
361 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
362 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
365 CriticalSectionScoped cs(send_critsect_);
366 max_payload_length_ = max_payload_length;
367 packet_over_head_ = packet_over_head;
371 uint16_t RTPSender::MaxDataPayloadLength() const {
374 CriticalSectionScoped rtx_lock(send_critsect_);
377 if (audio_configured_) {
378 return max_payload_length_ - RTPHeaderLength();
380 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
381 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
382 - ((rtx) ? 2 : 0); // RTX overhead.
386 uint16_t RTPSender::MaxPayloadLength() const {
387 return max_payload_length_;
390 uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
392 void RTPSender::SetRTXStatus(int mode) {
393 CriticalSectionScoped cs(send_critsect_);
397 void RTPSender::SetRtxSsrc(uint32_t ssrc) {
398 CriticalSectionScoped cs(send_critsect_);
402 uint32_t RTPSender::RtxSsrc() const {
403 CriticalSectionScoped cs(send_critsect_);
407 void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
408 int* payload_type) const {
409 CriticalSectionScoped cs(send_critsect_);
412 *payload_type = payload_type_rtx_;
415 void RTPSender::SetRtxPayloadType(int payload_type) {
416 CriticalSectionScoped cs(send_critsect_);
417 payload_type_rtx_ = payload_type;
420 int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
421 RtpVideoCodecTypes *video_type) {
422 CriticalSectionScoped cs(send_critsect_);
424 if (payload_type < 0) {
425 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
428 if (audio_configured_) {
429 int8_t red_pl_type = -1;
430 if (audio_->RED(red_pl_type) == 0) {
431 // We have configured RED.
432 if (red_pl_type == payload_type) {
433 // And it's a match...
438 if (payload_type_ == payload_type) {
439 if (!audio_configured_) {
440 *video_type = video_->VideoCodecType();
444 std::map<int8_t, RtpUtility::Payload*>::iterator it =
445 payload_type_map_.find(payload_type);
446 if (it == payload_type_map_.end()) {
447 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
450 SetSendPayloadType(payload_type);
451 RtpUtility::Payload* payload = it->second;
453 if (!payload->audio && !audio_configured_) {
454 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
455 *video_type = payload->typeSpecific.Video.videoCodecType;
456 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
461 int32_t RTPSender::SendOutgoingData(
462 const FrameType frame_type, const int8_t payload_type,
463 const uint32_t capture_timestamp, int64_t capture_time_ms,
464 const uint8_t *payload_data, const uint32_t payload_size,
465 const RTPFragmentationHeader *fragmentation,
466 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
469 // Drop this packet if we're not sending media packets.
470 CriticalSectionScoped cs(send_critsect_);
472 if (!sending_media_) {
476 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
477 if (CheckPayloadType(payload_type, &video_type) != 0) {
478 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
483 if (audio_configured_) {
484 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
485 "Send", "type", FrameTypeToString(frame_type));
486 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
487 frame_type == kFrameEmpty);
489 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
490 payload_data, payload_size, fragmentation);
492 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
493 "Send", "type", FrameTypeToString(frame_type));
494 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
496 if (frame_type == kFrameEmpty)
499 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
500 capture_timestamp, capture_time_ms,
501 payload_data, payload_size,
502 fragmentation, codec_info,
507 CriticalSectionScoped cs(statistics_crit_.get());
508 uint32_t frame_count = ++frame_counts_[frame_type];
509 if (frame_count_observer_) {
510 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
516 int RTPSender::TrySendRedundantPayloads(int bytes_to_send) {
518 CriticalSectionScoped cs(send_critsect_);
519 if ((rtx_ & kRtxRedundantPayloads) == 0)
523 uint8_t buffer[IP_PACKET_SIZE];
524 int bytes_left = bytes_to_send;
525 while (bytes_left > 0) {
526 uint16_t length = bytes_left;
527 int64_t capture_time_ms;
528 if (!packet_history_.GetBestFittingPacket(buffer, &length,
532 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
534 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
535 RTPHeader rtp_header;
536 rtp_parser.Parse(rtp_header);
537 bytes_left -= length - rtp_header.headerLength;
539 return bytes_to_send - bytes_left;
542 int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
544 int padding_bytes_in_packet = kMaxPaddingLength;
545 if (bytes < kMaxPaddingLength) {
546 padding_bytes_in_packet = bytes;
548 packet[0] |= 0x20; // Set padding bit.
550 reinterpret_cast<int32_t *>(&(packet[header_length]));
552 // Fill data buffer with random data.
553 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
554 data[j] = rand(); // NOLINT
556 // Set number of padding bytes in the last byte of the packet.
557 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
558 return padding_bytes_in_packet;
561 int RTPSender::TrySendPadData(int bytes) {
562 int64_t capture_time_ms;
565 CriticalSectionScoped cs(send_critsect_);
566 timestamp = timestamp_;
567 capture_time_ms = capture_time_ms_;
568 if (last_timestamp_time_ms_ > 0) {
570 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
572 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
575 return SendPadData(timestamp, capture_time_ms, bytes);
578 int RTPSender::SendPadData(uint32_t timestamp,
579 int64_t capture_time_ms,
581 int padding_bytes_in_packet = 0;
583 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
584 // Always send full padding packets.
585 if (bytes < kMaxPaddingLength)
586 bytes = kMaxPaddingLength;
589 uint16_t sequence_number;
593 CriticalSectionScoped cs(send_critsect_);
594 // Only send padding packets following the last packet of a frame,
595 // indicated by the marker bit.
596 if (rtx_ == kRtxOff) {
597 // Without RTX we can't send padding in the middle of frames.
598 if (!last_packet_marker_bit_)
601 sequence_number = sequence_number_;
603 payload_type = payload_type_;
606 // Without abs-send-time a media packet must be sent before padding so
607 // that the timestamps used for estimation are correct.
608 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
609 kRtpExtensionAbsoluteSendTime))
612 sequence_number = sequence_number_rtx_;
613 ++sequence_number_rtx_;
614 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
620 uint8_t padding_packet[IP_PACKET_SIZE];
621 int header_length = CreateRTPHeader(padding_packet,
629 padding_bytes_in_packet =
630 BuildPaddingPacket(padding_packet, header_length, bytes);
631 int length = padding_bytes_in_packet + header_length;
632 int64_t now_ms = clock_->TimeInMilliseconds();
634 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
635 RTPHeader rtp_header;
636 rtp_parser.Parse(rtp_header);
638 if (capture_time_ms > 0) {
639 UpdateTransmissionTimeOffset(
640 padding_packet, length, rtp_header, now_ms - capture_time_ms);
643 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
644 if (!SendPacketToNetwork(padding_packet, length))
646 bytes_sent += padding_bytes_in_packet;
647 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
653 void RTPSender::SetStorePacketsStatus(const bool enable,
654 const uint16_t number_to_store) {
655 packet_history_.SetStorePacketsStatus(enable, number_to_store);
658 bool RTPSender::StorePackets() const {
659 return packet_history_.StorePackets();
662 int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
663 uint16_t length = IP_PACKET_SIZE;
664 uint8_t data_buffer[IP_PACKET_SIZE];
665 int64_t capture_time_ms;
666 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
667 data_buffer, &length,
674 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
676 if (!rtp_parser.Parse(header)) {
680 // Convert from TickTime to Clock since capture_time_ms is based on
682 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
683 if (!paced_sender_->SendPacket(
684 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
685 corrected_capture_tims_ms, length - header.headerLength, true)) {
686 // We can't send the packet right now.
687 // We will be called when it is time.
693 CriticalSectionScoped lock(send_critsect_);
696 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
697 (rtx & kRtxRetransmitted) > 0, true) ?
701 bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
704 bytes_sent = transport_->SendPacket(id_, packet, size);
706 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
707 "size", size, "sent", bytes_sent);
708 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
709 if (bytes_sent <= 0) {
710 LOG(LS_WARNING) << "Transport failed to send packet";
716 int RTPSender::SelectiveRetransmissions() const {
719 return video_->SelectiveRetransmissions();
722 int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
725 return video_->SetSelectiveRetransmissions(settings);
728 void RTPSender::OnReceivedNACK(
729 const std::list<uint16_t>& nack_sequence_numbers,
730 const uint16_t avg_rtt) {
731 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
732 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
733 const int64_t now = clock_->TimeInMilliseconds();
734 uint32_t bytes_re_sent = 0;
735 uint32_t target_bitrate = GetTargetBitrate();
737 // Enough bandwidth to send NACK?
738 if (!ProcessNACKBitRate(now)) {
739 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
744 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
745 it != nack_sequence_numbers.end(); ++it) {
746 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
747 if (bytes_sent > 0) {
748 bytes_re_sent += bytes_sent;
749 } else if (bytes_sent == 0) {
750 // The packet has previously been resent.
751 // Try resending next packet in the list.
753 } else if (bytes_sent < 0) {
754 // Failed to send one Sequence number. Give up the rest in this nack.
755 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
756 << ", Discard rest of packets";
759 // Delay bandwidth estimate (RTT * BW).
760 if (target_bitrate != 0 && avg_rtt) {
761 // kbits/s * ms = bits => bits/8 = bytes
762 uint32_t target_bytes =
763 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
764 if (bytes_re_sent > target_bytes) {
765 break; // Ignore the rest of the packets in the list.
769 if (bytes_re_sent > 0) {
770 // TODO(pwestin) consolidate these two methods.
771 UpdateNACKBitRate(bytes_re_sent, now);
772 nack_bitrate_.Update(bytes_re_sent);
776 bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
779 const uint32_t kAvgIntervalMs = 1000;
780 uint32_t target_bitrate = GetTargetBitrate();
782 CriticalSectionScoped cs(send_critsect_);
784 if (target_bitrate == 0) {
787 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
788 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
789 // Don't use data older than 1sec.
792 byte_count += nack_byte_count_[num];
795 uint32_t time_interval = kAvgIntervalMs;
796 if (num == NACK_BYTECOUNT_SIZE) {
797 // More than NACK_BYTECOUNT_SIZE nack messages has been received
798 // during the last msg_interval.
799 if (nack_byte_count_times_[num - 1] <= now) {
800 time_interval = now - nack_byte_count_times_[num - 1];
803 return (byte_count * 8) <
804 static_cast<int>(target_bitrate / 1000 * time_interval);
807 void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
808 const uint32_t now) {
809 CriticalSectionScoped cs(send_critsect_);
811 // Save bitrate statistics.
814 // Add padding length.
815 nack_byte_count_[0] += bytes;
817 if (nack_byte_count_times_[0] == 0) {
821 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
822 nack_byte_count_[i + 1] = nack_byte_count_[i];
823 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
826 nack_byte_count_[0] = bytes;
827 nack_byte_count_times_[0] = now;
832 // Called from pacer when we can send the packet.
833 bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
834 int64_t capture_time_ms,
835 bool retransmission) {
836 uint16_t length = IP_PACKET_SIZE;
837 uint8_t data_buffer[IP_PACKET_SIZE];
838 int64_t stored_time_ms;
840 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
846 // Packet cannot be found. Allow sending to continue.
849 if (!retransmission && capture_time_ms > 0) {
850 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
854 CriticalSectionScoped lock(send_critsect_);
857 return PrepareAndSendPacket(data_buffer,
860 retransmission && (rtx & kRtxRetransmitted) > 0,
864 bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
866 int64_t capture_time_ms,
868 bool is_retransmit) {
869 uint8_t *buffer_to_send_ptr = buffer;
871 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
872 RTPHeader rtp_header;
873 rtp_parser.Parse(rtp_header);
874 if (!is_retransmit && rtp_header.markerBit) {
875 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
878 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
879 "timestamp", rtp_header.timestamp,
880 "seqnum", rtp_header.sequenceNumber);
882 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
884 BuildRtxPacket(buffer, &length, data_buffer_rtx);
885 buffer_to_send_ptr = data_buffer_rtx;
888 int64_t now_ms = clock_->TimeInMilliseconds();
889 int64_t diff_ms = now_ms - capture_time_ms;
890 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
892 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
893 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
895 CriticalSectionScoped lock(send_critsect_);
896 media_has_been_sent_ = true;
898 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
903 void RTPSender::UpdateRtpStats(const uint8_t* buffer,
905 const RTPHeader& header,
907 bool is_retransmit) {
908 StreamDataCounters* counters;
909 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
910 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
912 CriticalSectionScoped lock(statistics_crit_.get());
914 counters = &rtx_rtp_stats_;
916 counters = &rtp_stats_;
919 total_bitrate_sent_.Update(size);
921 if (IsFecPacket(buffer, header)) {
922 ++counters->fec_packets;
926 ++counters->retransmitted_packets;
928 counters->bytes += size - (header.headerLength + header.paddingLength);
929 counters->header_bytes += header.headerLength;
930 counters->padding_bytes += header.paddingLength;
933 if (rtp_stats_callback_) {
934 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
938 bool RTPSender::IsFecPacket(const uint8_t* buffer,
939 const RTPHeader& header) const {
946 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
947 return fec_enabled &&
948 header.payloadType == pt_red &&
949 buffer[header.headerLength] == pt_fec;
952 int RTPSender::TimeToSendPadding(int bytes) {
954 CriticalSectionScoped cs(send_critsect_);
955 if (!sending_media_) return 0;
957 int available_bytes = bytes;
958 if (available_bytes > 0)
959 available_bytes -= TrySendRedundantPayloads(available_bytes);
960 if (available_bytes > 0)
961 available_bytes -= TrySendPadData(available_bytes);
962 return bytes - available_bytes;
965 // TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
966 int32_t RTPSender::SendToNetwork(
967 uint8_t *buffer, int payload_length, int rtp_header_length,
968 int64_t capture_time_ms, StorageType storage,
969 PacedSender::Priority priority) {
970 RtpUtility::RtpHeaderParser rtp_parser(buffer,
971 payload_length + rtp_header_length);
972 RTPHeader rtp_header;
973 rtp_parser.Parse(rtp_header);
975 int64_t now_ms = clock_->TimeInMilliseconds();
977 // |capture_time_ms| <= 0 is considered invalid.
978 // TODO(holmer): This should be changed all over Video Engine so that negative
979 // time is consider invalid, while 0 is considered a valid time.
980 if (capture_time_ms > 0) {
981 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
982 rtp_header, now_ms - capture_time_ms);
985 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
988 // Used for NACK and to spread out the transmission of packets.
989 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
990 max_payload_length_, capture_time_ms,
995 if (paced_sender_ && storage != kDontStore) {
996 // Correct offset between implementations of millisecond time stamps in
997 // TickTime and Clock.
998 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
999 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
1000 rtp_header.sequenceNumber, corrected_time_ms,
1001 payload_length, false)) {
1002 if (last_capture_time_ms_sent_ == 0 ||
1003 corrected_time_ms > last_capture_time_ms_sent_) {
1004 last_capture_time_ms_sent_ = corrected_time_ms;
1005 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
1006 "capture_time_ms", corrected_time_ms);
1008 // We can't send the packet right now.
1009 // We will be called when it is time.
1013 if (capture_time_ms > 0) {
1014 UpdateDelayStatistics(capture_time_ms, now_ms);
1016 uint32_t length = payload_length + rtp_header_length;
1017 if (!SendPacketToNetwork(buffer, length))
1020 CriticalSectionScoped lock(send_critsect_);
1021 media_has_been_sent_ = true;
1023 UpdateRtpStats(buffer, length, rtp_header, false, false);
1027 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
1029 int avg_delay_ms = 0;
1030 int max_delay_ms = 0;
1032 CriticalSectionScoped lock(send_critsect_);
1036 CriticalSectionScoped cs(statistics_crit_.get());
1037 // TODO(holmer): Compute this iteratively instead.
1038 send_delays_[now_ms] = now_ms - capture_time_ms;
1039 send_delays_.erase(send_delays_.begin(),
1040 send_delays_.lower_bound(now_ms -
1041 kSendSideDelayWindowMs));
1043 if (send_side_delay_observer_ &&
1044 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1045 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1046 max_delay_ms, ssrc);
1050 void RTPSender::ProcessBitrate() {
1051 CriticalSectionScoped cs(send_critsect_);
1052 total_bitrate_sent_.Process();
1053 nack_bitrate_.Process();
1054 if (audio_configured_) {
1057 video_->ProcessBitrate();
1060 uint16_t RTPSender::RTPHeaderLength() const {
1061 CriticalSectionScoped lock(send_critsect_);
1062 uint16_t rtp_header_length = 12;
1063 if (include_csrcs_) {
1064 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
1066 rtp_header_length += RtpHeaderExtensionTotalLength();
1067 return rtp_header_length;
1070 uint16_t RTPSender::IncrementSequenceNumber() {
1071 CriticalSectionScoped cs(send_critsect_);
1072 return sequence_number_++;
1075 void RTPSender::ResetDataCounters() {
1079 CriticalSectionScoped ssrc_lock(send_critsect_);
1081 ssrc_rtx = ssrc_rtx_;
1083 CriticalSectionScoped lock(statistics_crit_.get());
1084 rtp_stats_ = StreamDataCounters();
1085 rtx_rtp_stats_ = StreamDataCounters();
1086 if (rtp_stats_callback_) {
1087 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1088 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
1092 void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1093 StreamDataCounters* rtx_stats) const {
1094 CriticalSectionScoped lock(statistics_crit_.get());
1095 *rtp_stats = rtp_stats_;
1096 *rtx_stats = rtx_rtp_stats_;
1099 int RTPSender::CreateRTPHeader(
1100 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1101 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1102 uint8_t num_csrcs) const {
1103 header[0] = 0x80; // version 2.
1104 header[1] = static_cast<uint8_t>(payload_type);
1106 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
1108 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1109 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1110 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
1111 int32_t rtp_header_length = 12;
1113 // Add the CSRCs if any.
1114 if (num_csrcs > 0) {
1115 if (num_csrcs > kRtpCsrcSize) {
1120 uint8_t *ptr = &header[rtp_header_length];
1121 for (int i = 0; i < num_csrcs; ++i) {
1122 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
1125 header[0] = (header[0] & 0xf0) | num_csrcs;
1127 // Update length of header.
1128 rtp_header_length += sizeof(uint32_t) * num_csrcs;
1131 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1133 header[0] |= 0x10; // Set extension bit.
1134 rtp_header_length += len;
1136 return rtp_header_length;
1139 int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1140 const int8_t payload_type,
1141 const bool marker_bit,
1142 const uint32_t capture_timestamp,
1143 int64_t capture_time_ms,
1144 const bool timestamp_provided,
1145 const bool inc_sequence_number) {
1146 assert(payload_type >= 0);
1147 CriticalSectionScoped cs(send_critsect_);
1149 if (timestamp_provided) {
1150 timestamp_ = start_timestamp_ + capture_timestamp;
1152 // Make a unique time stamp.
1153 // We can't inc by the actual time, since then we increase the risk of back
1157 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
1158 uint32_t sequence_number = sequence_number_++;
1159 capture_time_ms_ = capture_time_ms;
1160 last_packet_marker_bit_ = marker_bit;
1161 int csrcs_length = 0;
1163 csrcs_length = num_csrcs_;
1164 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1165 timestamp_, sequence_number, csrcs_, csrcs_length);
1168 uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
1169 if (rtp_header_extension_map_.Size() <= 0) {
1172 // RTP header extension, RFC 3550.
1174 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1175 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 // | defined by profile | length |
1177 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1178 // | header extension |
1181 const uint32_t kPosLength = 2;
1182 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
1184 // Add extension ID (0xBEDE).
1185 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
1188 uint16_t total_block_length = 0;
1190 RTPExtensionType type = rtp_header_extension_map_.First();
1191 while (type != kRtpExtensionNone) {
1192 uint8_t block_length = 0;
1194 case kRtpExtensionTransmissionTimeOffset:
1195 block_length = BuildTransmissionTimeOffsetExtension(
1196 data_buffer + kHeaderLength + total_block_length);
1198 case kRtpExtensionAudioLevel:
1199 block_length = BuildAudioLevelExtension(
1200 data_buffer + kHeaderLength + total_block_length);
1202 case kRtpExtensionAbsoluteSendTime:
1203 block_length = BuildAbsoluteSendTimeExtension(
1204 data_buffer + kHeaderLength + total_block_length);
1209 total_block_length += block_length;
1210 type = rtp_header_extension_map_.Next(type);
1212 if (total_block_length == 0) {
1213 // No extension added.
1216 // Set header length (in number of Word32, header excluded).
1217 assert(total_block_length % 4 == 0);
1218 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1219 total_block_length / 4);
1220 // Total added length.
1221 return kHeaderLength + total_block_length;
1224 uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1225 uint8_t* data_buffer) const {
1226 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1228 // The transmission time is signaled to the receiver in-band using the
1229 // general mechanism for RTP header extensions [RFC5285]. The payload
1230 // of this extension (the transmitted value) is a 24-bit signed integer.
1231 // When added to the RTP timestamp of the packet, it represents the
1232 // "effective" RTP transmission time of the packet, on the RTP
1235 // The form of the transmission offset extension block:
1238 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1239 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1240 // | ID | len=2 | transmission offset |
1241 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1243 // Get id defined by user.
1245 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1251 const uint8_t len = 2;
1252 data_buffer[pos++] = (id << 4) + len;
1253 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1254 transmission_time_offset_);
1256 assert(pos == kTransmissionTimeOffsetLength);
1257 return kTransmissionTimeOffsetLength;
1260 uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1261 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1263 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1265 // The form of the audio level extension block:
1268 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1269 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270 // | ID | len=0 |V| level | 0x00 | 0x00 |
1271 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1273 // Note that we always include 2 pad bytes, which will result in legal and
1274 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1275 // are implemented. Right now the pad bytes would anyway be required at end
1276 // of the extension block, so it makes no difference.
1278 // Get id defined by user.
1280 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1285 const uint8_t len = 0;
1286 data_buffer[pos++] = (id << 4) + len;
1287 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1288 data_buffer[pos++] = 0; // Padding.
1289 data_buffer[pos++] = 0; // Padding.
1290 // kAudioLevelLength is including pad bytes.
1291 assert(pos == kAudioLevelLength);
1292 return kAudioLevelLength;
1295 uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
1296 // Absolute send time in RTP streams.
1298 // The absolute send time is signaled to the receiver in-band using the
1299 // general mechanism for RTP header extensions [RFC5285]. The payload
1300 // of this extension (the transmitted value) is a 24-bit unsigned integer
1301 // containing the sender's current time in seconds as a fixed point number
1302 // with 18 bits fractional part.
1304 // The form of the absolute send time extension block:
1307 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1308 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1309 // | ID | len=2 | absolute send time |
1310 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1312 // Get id defined by user.
1314 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1320 const uint8_t len = 2;
1321 data_buffer[pos++] = (id << 4) + len;
1322 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
1324 assert(pos == kAbsoluteSendTimeLength);
1325 return kAbsoluteSendTimeLength;
1328 void RTPSender::UpdateTransmissionTimeOffset(
1329 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1330 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
1331 CriticalSectionScoped cs(send_critsect_);
1334 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1339 // Get length until start of header extension block.
1340 int extension_block_pos =
1341 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1342 kRtpExtensionTransmissionTimeOffset);
1343 if (extension_block_pos < 0) {
1345 << "Failed to update transmission time offset, not registered.";
1348 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1349 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
1350 rtp_header.headerLength <
1351 block_pos + kTransmissionTimeOffsetLength) {
1353 << "Failed to update transmission time offset, invalid length.";
1356 // Verify that header contains extension.
1357 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1358 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1359 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1360 "extension not found.";
1363 // Verify first byte in block.
1364 const uint8_t first_block_byte = (id << 4) + 2;
1365 if (rtp_packet[block_pos] != first_block_byte) {
1366 LOG(LS_WARNING) << "Failed to update transmission time offset.";
1369 // Update transmission offset field (converting to a 90 kHz timestamp).
1370 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1371 time_diff_ms * 90); // RTP timestamp.
1374 bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1375 const uint16_t rtp_packet_length,
1376 const RTPHeader &rtp_header,
1377 const bool is_voiced,
1378 const uint8_t dBov) const {
1379 CriticalSectionScoped cs(send_critsect_);
1383 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1387 // Get length until start of header extension block.
1388 int extension_block_pos =
1389 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1390 kRtpExtensionAudioLevel);
1391 if (extension_block_pos < 0) {
1392 // The feature is not enabled.
1395 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1396 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1397 rtp_header.headerLength < block_pos + kAudioLevelLength) {
1398 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
1401 // Verify that header contains extension.
1402 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1403 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1404 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
1407 // Verify first byte in block.
1408 const uint8_t first_block_byte = (id << 4) + 0;
1409 if (rtp_packet[block_pos] != first_block_byte) {
1410 LOG(LS_WARNING) << "Failed to update audio level.";
1413 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1417 void RTPSender::UpdateAbsoluteSendTime(
1418 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1419 const RTPHeader &rtp_header, const int64_t now_ms) const {
1420 CriticalSectionScoped cs(send_critsect_);
1424 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1429 // Get length until start of header extension block.
1430 int extension_block_pos =
1431 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1432 kRtpExtensionAbsoluteSendTime);
1433 if (extension_block_pos < 0) {
1434 // The feature is not enabled.
1437 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1438 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
1439 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
1440 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
1443 // Verify that header contains extension.
1444 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1445 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1447 << "Failed to update absolute send time, hdr extension not found.";
1450 // Verify first byte in block.
1451 const uint8_t first_block_byte = (id << 4) + 2;
1452 if (rtp_packet[block_pos] != first_block_byte) {
1453 LOG(LS_WARNING) << "Failed to update absolute send time.";
1456 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1457 // fractional part).
1458 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1459 ((now_ms << 18) / 1000) & 0x00ffffff);
1462 void RTPSender::SetSendingStatus(bool enabled) {
1464 uint32_t frequency_hz = SendPayloadFrequency();
1465 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
1467 // Will be ignored if it's already configured via API.
1468 SetStartTimestamp(RTPtime, false);
1470 CriticalSectionScoped lock(send_critsect_);
1471 if (!ssrc_forced_) {
1472 // Generate a new SSRC.
1473 ssrc_db_.ReturnSSRC(ssrc_);
1474 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1475 bitrates_->set_ssrc(ssrc_);
1477 // Don't initialize seq number if SSRC passed externally.
1478 if (!sequence_number_forced_ && !ssrc_forced_) {
1479 // Generate a new sequence number.
1481 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
1486 void RTPSender::SetSendingMediaStatus(const bool enabled) {
1487 CriticalSectionScoped cs(send_critsect_);
1488 sending_media_ = enabled;
1491 bool RTPSender::SendingMedia() const {
1492 CriticalSectionScoped cs(send_critsect_);
1493 return sending_media_;
1496 uint32_t RTPSender::Timestamp() const {
1497 CriticalSectionScoped cs(send_critsect_);
1501 void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
1502 CriticalSectionScoped cs(send_critsect_);
1504 start_timestamp_forced_ = true;
1505 start_timestamp_ = timestamp;
1507 if (!start_timestamp_forced_) {
1508 start_timestamp_ = timestamp;
1513 uint32_t RTPSender::StartTimestamp() const {
1514 CriticalSectionScoped cs(send_critsect_);
1515 return start_timestamp_;
1518 uint32_t RTPSender::GenerateNewSSRC() {
1519 // If configured via API, return 0.
1520 CriticalSectionScoped cs(send_critsect_);
1525 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1526 bitrates_->set_ssrc(ssrc_);
1530 void RTPSender::SetSSRC(uint32_t ssrc) {
1531 // This is configured via the API.
1532 CriticalSectionScoped cs(send_critsect_);
1534 if (ssrc_ == ssrc && ssrc_forced_) {
1535 return; // Since it's same ssrc, don't reset anything.
1537 ssrc_forced_ = true;
1538 ssrc_db_.ReturnSSRC(ssrc_);
1539 ssrc_db_.RegisterSSRC(ssrc);
1541 bitrates_->set_ssrc(ssrc_);
1542 if (!sequence_number_forced_) {
1544 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
1548 uint32_t RTPSender::SSRC() const {
1549 CriticalSectionScoped cs(send_critsect_);
1553 void RTPSender::SetCSRCStatus(const bool include) {
1554 CriticalSectionScoped lock(send_critsect_);
1555 include_csrcs_ = include;
1558 void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1559 const uint8_t arr_length) {
1560 assert(arr_length <= kRtpCsrcSize);
1561 CriticalSectionScoped cs(send_critsect_);
1563 for (int i = 0; i < arr_length; i++) {
1564 csrcs_[i] = arr_of_csrc[i];
1566 num_csrcs_ = arr_length;
1569 int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
1570 assert(arr_of_csrc);
1571 CriticalSectionScoped cs(send_critsect_);
1572 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1573 arr_of_csrc[i] = csrcs_[i];
1578 void RTPSender::SetSequenceNumber(uint16_t seq) {
1579 CriticalSectionScoped cs(send_critsect_);
1580 sequence_number_forced_ = true;
1581 sequence_number_ = seq;
1584 uint16_t RTPSender::SequenceNumber() const {
1585 CriticalSectionScoped cs(send_critsect_);
1586 return sequence_number_;
1590 int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1591 const uint16_t time_ms,
1592 const uint8_t level) {
1593 if (!audio_configured_) {
1596 return audio_->SendTelephoneEvent(key, time_ms, level);
1599 bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
1600 if (!audio_configured_) {
1603 return audio_->SendTelephoneEventActive(*telephone_event);
1606 int32_t RTPSender::SetAudioPacketSize(
1607 const uint16_t packet_size_samples) {
1608 if (!audio_configured_) {
1611 return audio_->SetAudioPacketSize(packet_size_samples);
1614 int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
1615 return audio_->SetAudioLevel(level_d_bov);
1618 int32_t RTPSender::SetRED(const int8_t payload_type) {
1619 if (!audio_configured_) {
1622 return audio_->SetRED(payload_type);
1625 int32_t RTPSender::RED(int8_t *payload_type) const {
1626 if (!audio_configured_) {
1629 return audio_->RED(*payload_type);
1633 VideoCodecInformation *RTPSender::CodecInformationVideo() {
1634 if (audio_configured_) {
1637 return video_->CodecInformationVideo();
1640 RtpVideoCodecTypes RTPSender::VideoCodecType() const {
1641 assert(!audio_configured_ && "Sender is an audio stream!");
1642 return video_->VideoCodecType();
1645 uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
1646 if (audio_configured_) {
1649 return video_->MaxConfiguredBitrateVideo();
1652 int32_t RTPSender::SendRTPIntraRequest() {
1653 if (audio_configured_) {
1656 return video_->SendRTPIntraRequest();
1659 int32_t RTPSender::SetGenericFECStatus(
1660 const bool enable, const uint8_t payload_type_red,
1661 const uint8_t payload_type_fec) {
1662 if (audio_configured_) {
1665 return video_->SetGenericFECStatus(enable, payload_type_red,
1669 int32_t RTPSender::GenericFECStatus(
1670 bool *enable, uint8_t *payload_type_red,
1671 uint8_t *payload_type_fec) const {
1672 if (audio_configured_) {
1675 return video_->GenericFECStatus(
1676 *enable, *payload_type_red, *payload_type_fec);
1679 int32_t RTPSender::SetFecParameters(
1680 const FecProtectionParams *delta_params,
1681 const FecProtectionParams *key_params) {
1682 if (audio_configured_) {
1685 return video_->SetFecParameters(delta_params, key_params);
1688 void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1689 uint8_t* buffer_rtx) {
1690 CriticalSectionScoped cs(send_critsect_);
1691 uint8_t* data_buffer_rtx = buffer_rtx;
1693 RtpUtility::RtpHeaderParser rtp_parser(
1694 reinterpret_cast<const uint8_t*>(buffer), *length);
1696 RTPHeader rtp_header;
1697 rtp_parser.Parse(rtp_header);
1699 // Add original RTP header.
1700 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
1702 // Replace payload type, if a specific type is set for RTX.
1703 if (payload_type_rtx_ != -1) {
1704 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
1705 if (rtp_header.markerBit)
1706 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1709 // Replace sequence number.
1710 uint8_t *ptr = data_buffer_rtx + 2;
1711 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1715 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1717 // Add OSN (original sequence number).
1718 ptr = data_buffer_rtx + rtp_header.headerLength;
1719 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
1722 // Add original payload data.
1723 memcpy(ptr, buffer + rtp_header.headerLength,
1724 *length - rtp_header.headerLength);
1728 void RTPSender::RegisterRtpStatisticsCallback(
1729 StreamDataCountersCallback* callback) {
1730 CriticalSectionScoped cs(statistics_crit_.get());
1731 rtp_stats_callback_ = callback;
1734 StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1735 CriticalSectionScoped cs(statistics_crit_.get());
1736 return rtp_stats_callback_;
1739 uint32_t RTPSender::BitrateSent() const {
1740 return total_bitrate_sent_.BitrateLast();
1743 void RTPSender::SetRtpState(const RtpState& rtp_state) {
1744 SetStartTimestamp(rtp_state.start_timestamp, true);
1745 CriticalSectionScoped lock(send_critsect_);
1746 sequence_number_ = rtp_state.sequence_number;
1747 sequence_number_forced_ = true;
1748 timestamp_ = rtp_state.timestamp;
1749 capture_time_ms_ = rtp_state.capture_time_ms;
1750 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1751 media_has_been_sent_ = rtp_state.media_has_been_sent;
1754 RtpState RTPSender::GetRtpState() const {
1755 CriticalSectionScoped lock(send_critsect_);
1758 state.sequence_number = sequence_number_;
1759 state.start_timestamp = start_timestamp_;
1760 state.timestamp = timestamp_;
1761 state.capture_time_ms = capture_time_ms_;
1762 state.last_timestamp_time_ms = last_timestamp_time_ms_;
1763 state.media_has_been_sent = media_has_been_sent_;
1768 void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1769 CriticalSectionScoped lock(send_critsect_);
1770 sequence_number_rtx_ = rtp_state.sequence_number;
1773 RtpState RTPSender::GetRtxRtpState() const {
1774 CriticalSectionScoped lock(send_critsect_);
1777 state.sequence_number = sequence_number_rtx_;
1778 state.start_timestamp = start_timestamp_;
1783 } // namespace webrtc