1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/rtcp/rtcp_sender.h"
10 #include "base/logging.h"
11 #include "media/cast/cast_environment.h"
12 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h"
13 #include "media/cast/rtcp/rtcp_defines.h"
14 #include "media/cast/rtcp/rtcp_utility.h"
15 #include "media/cast/transport/cast_transport_defines.h"
16 #include "media/cast/transport/pacing/paced_sender.h"
17 #include "net/base/big_endian.h"
21 using media::cast::kRtcpCastLogHeaderSize
;
22 using media::cast::kRtcpSenderFrameLogSize
;
23 using media::cast::kRtcpReceiverFrameLogSize
;
24 using media::cast::kRtcpReceiverEventLogSize
;
26 // Converts a log event type to an integer value.
27 int ConvertEventTypeToWireFormat(const media::cast::CastLoggingEvent
& event
) {
29 case media::cast::kAudioAckSent
:
31 case media::cast::kAudioPlayoutDelay
:
33 case media::cast::kAudioFrameDecoded
:
35 case media::cast::kAudioPacketReceived
:
37 case media::cast::kVideoAckSent
:
39 case media::cast::kVideoFrameDecoded
:
41 case media::cast::kVideoRenderDelay
:
43 case media::cast::kVideoPacketReceived
:
45 case media::cast::kDuplicatePacketReceived
:
48 return 0; // Not an interesting event.
52 uint16
MergeEventTypeAndTimestampForWireFormat(
53 const media::cast::CastLoggingEvent
& event
,
54 const base::TimeDelta
& time_delta
) {
55 int64 time_delta_ms
= time_delta
.InMilliseconds();
56 // Max delta is 4096 milliseconds.
57 DCHECK_GE(GG_INT64_C(0xfff), time_delta_ms
);
59 uint16 event_type_and_timestamp_delta
=
60 static_cast<uint16
>(time_delta_ms
& 0xfff);
62 uint16 event_type
= ConvertEventTypeToWireFormat(event
);
64 DCHECK(!(event_type
& 0xfff0));
65 return (event_type
<< 12) + event_type_and_timestamp_delta
;
68 bool ScanRtcpReceiverLogMessage(
69 const media::cast::RtcpReceiverLogMessage
& receiver_log_message
,
70 size_t start_size
, size_t* number_of_frames
,
71 size_t* total_number_of_messages_to_send
, size_t* rtcp_log_size
) {
72 if (receiver_log_message
.empty()) return false;
74 size_t remaining_space
= media::cast::kMaxIpPacketSize
- start_size
;
76 // We must have space for at least one message
77 DCHECK_GE(remaining_space
, kRtcpCastLogHeaderSize
+
78 kRtcpReceiverFrameLogSize
+
79 kRtcpReceiverEventLogSize
)
80 << "Not enough buffer space";
82 if (remaining_space
< kRtcpCastLogHeaderSize
+ kRtcpReceiverFrameLogSize
+
83 kRtcpReceiverEventLogSize
) {
86 // Account for the RTCP header for an application-defined packet.
87 remaining_space
-= kRtcpCastLogHeaderSize
;
89 media::cast::RtcpReceiverLogMessage::const_iterator frame_it
=
90 receiver_log_message
.begin();
91 for (; frame_it
!= receiver_log_message
.end(); ++frame_it
) {
92 (*number_of_frames
)++;
94 remaining_space
-= kRtcpReceiverFrameLogSize
;
96 size_t messages_in_frame
= frame_it
->event_log_messages_
.size();
97 size_t remaining_space_in_messages
=
98 remaining_space
/ kRtcpReceiverEventLogSize
;
99 size_t messages_to_send
=
100 std::min(messages_in_frame
, remaining_space_in_messages
);
101 if (messages_to_send
> media::cast::kRtcpMaxReceiverLogMessages
) {
102 // We can't send more than 256 messages.
104 media::cast::kRtcpMaxReceiverLogMessages
* kRtcpReceiverEventLogSize
;
105 *total_number_of_messages_to_send
+=
106 media::cast::kRtcpMaxReceiverLogMessages
;
109 remaining_space
-= messages_to_send
* kRtcpReceiverEventLogSize
;
110 *total_number_of_messages_to_send
+= messages_to_send
;
112 if (remaining_space
<
113 kRtcpReceiverFrameLogSize
+ kRtcpReceiverEventLogSize
) {
114 // Make sure that we have room for at least one more message.
119 kRtcpCastLogHeaderSize
+ *number_of_frames
* kRtcpReceiverFrameLogSize
+
120 *total_number_of_messages_to_send
* kRtcpReceiverEventLogSize
;
121 DCHECK_GE(media::cast::kMaxIpPacketSize
, start_size
+ *rtcp_log_size
)
122 << "Not enough buffer space";
124 VLOG(1) << "number of frames " << *number_of_frames
;
125 VLOG(1) << "total messages to send " << *total_number_of_messages_to_send
;
126 VLOG(1) << "rtcp log size " << *rtcp_log_size
;
134 // TODO(mikhal): This is only used by the receiver. Consider renaming.
135 RtcpSender::RtcpSender(scoped_refptr
<CastEnvironment
> cast_environment
,
136 transport::PacedPacketSender
* outgoing_transport
,
137 uint32 sending_ssrc
, const std::string
& c_name
)
138 : ssrc_(sending_ssrc
),
140 transport_(outgoing_transport
),
141 cast_environment_(cast_environment
) {
142 DCHECK_LT(c_name_
.length(), kRtcpCnameSize
) << "Invalid config";
145 RtcpSender::~RtcpSender() {}
148 bool RtcpSender::IsReceiverEvent(const media::cast::CastLoggingEvent
& event
) {
149 return ConvertEventTypeToWireFormat(event
) != 0;
152 void RtcpSender::SendRtcpFromRtpReceiver(
153 uint32 packet_type_flags
,
154 const transport::RtcpReportBlock
* report_block
,
155 const RtcpReceiverReferenceTimeReport
* rrtr
,
156 const RtcpCastMessage
* cast_message
,
157 ReceiverRtcpEventSubscriber
* event_subscriber
) {
158 if (packet_type_flags
& kRtcpSr
|| packet_type_flags
& kRtcpDlrr
||
159 packet_type_flags
& kRtcpSenderLog
) {
160 NOTREACHED() << "Invalid argument";
162 if (packet_type_flags
& kRtcpPli
|| packet_type_flags
& kRtcpRpsi
||
163 packet_type_flags
& kRtcpRemb
|| packet_type_flags
& kRtcpNack
) {
164 // Implement these for webrtc interop.
168 packet
.reserve(kMaxIpPacketSize
);
170 if (packet_type_flags
& kRtcpRr
) {
171 BuildRR(report_block
, &packet
);
172 if (!c_name_
.empty()) {
176 if (packet_type_flags
& kRtcpBye
) {
179 if (packet_type_flags
& kRtcpRrtr
) {
180 DCHECK(rrtr
) << "Invalid argument";
181 BuildRrtr(rrtr
, &packet
);
183 if (packet_type_flags
& kRtcpCast
) {
184 DCHECK(cast_message
) << "Invalid argument";
185 BuildCast(cast_message
, &packet
);
187 if (packet_type_flags
& kRtcpReceiverLog
) {
188 DCHECK(event_subscriber
) << "Invalid argument";
189 RtcpReceiverLogMessage receiver_log
;
190 event_subscriber
->GetReceiverLogMessageAndReset(&receiver_log
);
191 BuildReceiverLog(&receiver_log
, &packet
);
193 if (packet
.empty()) return; // Sanity don't send empty packets.
195 transport_
->SendRtcpPacket(packet
);
198 void RtcpSender::BuildRR(const transport::RtcpReportBlock
* report_block
,
199 Packet
* packet
) const {
200 size_t start_size
= packet
->size();
201 DCHECK_LT(start_size
+ 32, kMaxIpPacketSize
) << "Not enough buffer space";
202 if (start_size
+ 32 > kMaxIpPacketSize
) return;
204 uint16 number_of_rows
= (report_block
) ? 7 : 1;
205 packet
->resize(start_size
+ 8);
207 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 8);
208 big_endian_writer
.WriteU8(0x80 + (report_block
? 1 : 0));
209 big_endian_writer
.WriteU8(transport::kPacketTypeReceiverReport
);
210 big_endian_writer
.WriteU16(number_of_rows
);
211 big_endian_writer
.WriteU32(ssrc_
);
214 AddReportBlocks(*report_block
, packet
); // Adds 24 bytes.
218 void RtcpSender::AddReportBlocks(const transport::RtcpReportBlock
& report_block
,
219 Packet
* packet
) const {
220 size_t start_size
= packet
->size();
221 DCHECK_LT(start_size
+ 24, kMaxIpPacketSize
) << "Not enough buffer space";
222 if (start_size
+ 24 > kMaxIpPacketSize
) return;
224 packet
->resize(start_size
+ 24);
226 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 24);
227 big_endian_writer
.WriteU32(report_block
.media_ssrc
);
228 big_endian_writer
.WriteU8(report_block
.fraction_lost
);
229 big_endian_writer
.WriteU8(report_block
.cumulative_lost
>> 16);
230 big_endian_writer
.WriteU8(report_block
.cumulative_lost
>> 8);
231 big_endian_writer
.WriteU8(report_block
.cumulative_lost
);
233 // Extended highest seq_no, contain the highest sequence number received.
234 big_endian_writer
.WriteU32(report_block
.extended_high_sequence_number
);
235 big_endian_writer
.WriteU32(report_block
.jitter
);
237 // Last SR timestamp; our NTP time when we received the last report.
238 // This is the value that we read from the send report packet not when we
240 big_endian_writer
.WriteU32(report_block
.last_sr
);
242 // Delay since last received report, time since we received the report.
243 big_endian_writer
.WriteU32(report_block
.delay_since_last_sr
);
246 void RtcpSender::BuildSdec(Packet
* packet
) const {
247 size_t start_size
= packet
->size();
248 DCHECK_LT(start_size
+ 12 + c_name_
.length(), kMaxIpPacketSize
)
249 << "Not enough buffer space";
250 if (start_size
+ 12 > kMaxIpPacketSize
) return;
252 // SDES Source Description.
253 packet
->resize(start_size
+ 10);
255 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 10);
256 // We always need to add one SDES CNAME.
257 big_endian_writer
.WriteU8(0x80 + 1);
258 big_endian_writer
.WriteU8(transport::kPacketTypeSdes
);
260 // Handle SDES length later on.
261 uint32 sdes_length_position
= static_cast<uint32
>(start_size
) + 3;
262 big_endian_writer
.WriteU16(0);
263 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
264 big_endian_writer
.WriteU8(1); // CNAME = 1
265 big_endian_writer
.WriteU8(static_cast<uint8
>(c_name_
.length()));
267 size_t sdes_length
= 10 + c_name_
.length();
268 packet
->insert(packet
->end(), c_name_
.c_str(),
269 c_name_
.c_str() + c_name_
.length());
273 // We must have a zero field even if we have an even multiple of 4 bytes.
274 if ((packet
->size() % 4) == 0) {
276 packet
->push_back(0);
278 while ((packet
->size() % 4) != 0) {
280 packet
->push_back(0);
282 sdes_length
+= padding
;
284 // In 32-bit words minus one and we don't count the header.
285 uint8 buffer_length
= static_cast<uint8
>((sdes_length
/ 4) - 1);
286 (*packet
)[sdes_length_position
] = buffer_length
;
289 void RtcpSender::BuildPli(uint32 remote_ssrc
, Packet
* packet
) const {
290 size_t start_size
= packet
->size();
291 DCHECK_LT(start_size
+ 12, kMaxIpPacketSize
) << "Not enough buffer space";
292 if (start_size
+ 12 > kMaxIpPacketSize
) return;
294 packet
->resize(start_size
+ 12);
296 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 12);
297 uint8 FMT
= 1; // Picture loss indicator.
298 big_endian_writer
.WriteU8(0x80 + FMT
);
299 big_endian_writer
.WriteU8(transport::kPacketTypePayloadSpecific
);
300 big_endian_writer
.WriteU16(2); // Used fixed length of 2.
301 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
302 big_endian_writer
.WriteU32(remote_ssrc
); // Add the remote SSRC.
307 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
308 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 | PB |0| Payload Type| Native Rpsi bit string |
310 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 | defined per codec ... | Padding (0) |
312 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 void RtcpSender::BuildRpsi(const RtcpRpsiMessage
* rpsi
, Packet
* packet
) const {
315 size_t start_size
= packet
->size();
316 DCHECK_LT(start_size
+ 24, kMaxIpPacketSize
) << "Not enough buffer space";
317 if (start_size
+ 24 > kMaxIpPacketSize
) return;
319 packet
->resize(start_size
+ 24);
321 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 24);
322 uint8 FMT
= 3; // Reference Picture Selection Indication.
323 big_endian_writer
.WriteU8(0x80 + FMT
);
324 big_endian_writer
.WriteU8(transport::kPacketTypePayloadSpecific
);
327 uint32 bits_required
= 7;
328 uint8 bytes_required
= 1;
329 while ((rpsi
->picture_id
>> bits_required
) > 0) {
334 if (bytes_required
> 6) {
336 } else if (bytes_required
> 2) {
339 big_endian_writer
.WriteU8(0);
340 big_endian_writer
.WriteU8(size
);
341 big_endian_writer
.WriteU32(ssrc_
);
342 big_endian_writer
.WriteU32(rpsi
->remote_ssrc
);
344 uint8 padding_bytes
= 4 - ((2 + bytes_required
) % 4);
345 if (padding_bytes
== 4) {
348 // Add padding length in bits, padding can be 0, 8, 16 or 24.
349 big_endian_writer
.WriteU8(padding_bytes
* 8);
350 big_endian_writer
.WriteU8(rpsi
->payload_type
);
353 for (int i
= bytes_required
- 1; i
> 0; i
--) {
354 big_endian_writer
.WriteU8(0x80 |
355 static_cast<uint8
>(rpsi
->picture_id
>> (i
* 7)));
357 // Add last byte of picture ID.
358 big_endian_writer
.WriteU8(static_cast<uint8
>(rpsi
->picture_id
& 0x7f));
361 for (int j
= 0; j
< padding_bytes
; ++j
) {
362 big_endian_writer
.WriteU8(0);
366 void RtcpSender::BuildRemb(const RtcpRembMessage
* remb
, Packet
* packet
) const {
367 size_t start_size
= packet
->size();
368 size_t remb_size
= 20 + 4 * remb
->remb_ssrcs
.size();
369 DCHECK_LT(start_size
+ remb_size
, kMaxIpPacketSize
)
370 << "Not enough buffer space";
371 if (start_size
+ remb_size
> kMaxIpPacketSize
) return;
373 packet
->resize(start_size
+ remb_size
);
375 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), remb_size
);
377 // Add application layer feedback.
379 big_endian_writer
.WriteU8(0x80 + FMT
);
380 big_endian_writer
.WriteU8(transport::kPacketTypePayloadSpecific
);
381 big_endian_writer
.WriteU8(0);
382 big_endian_writer
.WriteU8(static_cast<uint8
>(remb
->remb_ssrcs
.size() + 4));
383 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
384 big_endian_writer
.WriteU32(0); // Remote SSRC must be 0.
385 big_endian_writer
.WriteU32(kRemb
);
386 big_endian_writer
.WriteU8(static_cast<uint8
>(remb
->remb_ssrcs
.size()));
388 // 6 bit exponent and a 18 bit mantissa.
389 uint8 bitrate_exponent
;
390 uint32 bitrate_mantissa
;
391 BitrateToRembExponentBitrate(remb
->remb_bitrate
, &bitrate_exponent
,
394 big_endian_writer
.WriteU8(static_cast<uint8
>(
395 (bitrate_exponent
<< 2) + ((bitrate_mantissa
>> 16) & 0x03)));
396 big_endian_writer
.WriteU8(static_cast<uint8
>(bitrate_mantissa
>> 8));
397 big_endian_writer
.WriteU8(static_cast<uint8
>(bitrate_mantissa
));
399 std::list
<uint32
>::const_iterator it
= remb
->remb_ssrcs
.begin();
400 for (; it
!= remb
->remb_ssrcs
.end(); ++it
) {
401 big_endian_writer
.WriteU32(*it
);
403 base::TimeTicks now
= cast_environment_
->Clock()->NowTicks();
404 cast_environment_
->Logging()->InsertGenericEvent(now
, kRembBitrate
,
408 void RtcpSender::BuildNack(const RtcpNackMessage
* nack
, Packet
* packet
) const {
409 size_t start_size
= packet
->size();
410 DCHECK_LT(start_size
+ 16, kMaxIpPacketSize
) << "Not enough buffer space";
411 if (start_size
+ 16 > kMaxIpPacketSize
) return;
413 packet
->resize(start_size
+ 16);
415 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 16);
418 big_endian_writer
.WriteU8(0x80 + FMT
);
419 big_endian_writer
.WriteU8(transport::kPacketTypeGenericRtpFeedback
);
420 big_endian_writer
.WriteU8(0);
421 size_t nack_size_pos
= start_size
+ 3;
422 big_endian_writer
.WriteU8(3);
423 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
424 big_endian_writer
.WriteU32(nack
->remote_ssrc
); // Add the remote SSRC.
426 // Build NACK bitmasks and write them to the Rtcp message.
427 // The nack list should be sorted and not contain duplicates.
428 size_t number_of_nack_fields
= 0;
429 size_t max_number_of_nack_fields
= std::min
<size_t>(
430 kRtcpMaxNackFields
, (kMaxIpPacketSize
- packet
->size()) / 4);
432 std::list
<uint16
>::const_iterator it
= nack
->nack_list
.begin();
433 while (it
!= nack
->nack_list
.end() &&
434 number_of_nack_fields
< max_number_of_nack_fields
) {
435 uint16 nack_sequence_number
= *it
;
438 while (it
!= nack
->nack_list
.end()) {
439 int shift
= static_cast<uint16
>(*it
- nack_sequence_number
) - 1;
440 if (shift
>= 0 && shift
<= 15) {
441 bitmask
|= (1 << shift
);
447 // Write the sequence number and the bitmask to the packet.
448 start_size
= packet
->size();
449 DCHECK_LT(start_size
+ 4, kMaxIpPacketSize
) << "Not enough buffer space";
450 if (start_size
+ 4 > kMaxIpPacketSize
) return;
452 packet
->resize(start_size
+ 4);
453 net::BigEndianWriter
big_endian_nack_writer(&((*packet
)[start_size
]), 4);
454 big_endian_nack_writer
.WriteU16(nack_sequence_number
);
455 big_endian_nack_writer
.WriteU16(bitmask
);
456 number_of_nack_fields
++;
458 DCHECK_GE(kRtcpMaxNackFields
, number_of_nack_fields
);
459 (*packet
)[nack_size_pos
] = static_cast<uint8
>(2 + number_of_nack_fields
);
462 void RtcpSender::BuildBye(Packet
* packet
) const {
463 size_t start_size
= packet
->size();
464 DCHECK_LT(start_size
+ 8, kMaxIpPacketSize
) << "Not enough buffer space";
465 if (start_size
+ 8 > kMaxIpPacketSize
) return;
467 packet
->resize(start_size
+ 8);
469 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 8);
470 big_endian_writer
.WriteU8(0x80 + 1);
471 big_endian_writer
.WriteU8(transport::kPacketTypeBye
);
472 big_endian_writer
.WriteU16(1); // Length.
473 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
476 void RtcpSender::BuildRrtr(const RtcpReceiverReferenceTimeReport
* rrtr
,
477 Packet
* packet
) const {
478 size_t start_size
= packet
->size();
479 DCHECK_LT(start_size
+ 20, kMaxIpPacketSize
) << "Not enough buffer space";
480 if (start_size
+ 20 > kMaxIpPacketSize
) return;
482 packet
->resize(start_size
+ 20);
484 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 20);
486 big_endian_writer
.WriteU8(0x80);
487 big_endian_writer
.WriteU8(transport::kPacketTypeXr
);
488 big_endian_writer
.WriteU16(4); // Length.
489 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
490 big_endian_writer
.WriteU8(4); // Add block type.
491 big_endian_writer
.WriteU8(0); // Add reserved.
492 big_endian_writer
.WriteU16(2); // Block length.
494 // Add the media (received RTP) SSRC.
495 big_endian_writer
.WriteU32(rrtr
->ntp_seconds
);
496 big_endian_writer
.WriteU32(rrtr
->ntp_fraction
);
499 void RtcpSender::BuildCast(const RtcpCastMessage
* cast
, Packet
* packet
) const {
500 size_t start_size
= packet
->size();
501 DCHECK_LT(start_size
+ 20, kMaxIpPacketSize
) << "Not enough buffer space";
502 if (start_size
+ 20 > kMaxIpPacketSize
) return;
504 packet
->resize(start_size
+ 20);
506 net::BigEndianWriter
big_endian_writer(&((*packet
)[start_size
]), 20);
507 uint8 FMT
= 15; // Application layer feedback.
508 big_endian_writer
.WriteU8(0x80 + FMT
);
509 big_endian_writer
.WriteU8(transport::kPacketTypePayloadSpecific
);
510 big_endian_writer
.WriteU8(0);
511 size_t cast_size_pos
= start_size
+ 3; // Save length position.
512 big_endian_writer
.WriteU8(4);
513 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
514 big_endian_writer
.WriteU32(cast
->media_ssrc_
); // Remote SSRC.
515 big_endian_writer
.WriteU32(kCast
);
516 big_endian_writer
.WriteU8(static_cast<uint8
>(cast
->ack_frame_id_
));
517 size_t cast_loss_field_pos
= start_size
+ 17; // Save loss field position.
518 big_endian_writer
.WriteU8(0); // Overwritten with number_of_loss_fields.
519 big_endian_writer
.WriteU8(0); // Reserved.
520 big_endian_writer
.WriteU8(0); // Reserved.
522 size_t number_of_loss_fields
= 0;
523 size_t max_number_of_loss_fields
= std::min
<size_t>(
524 kRtcpMaxCastLossFields
, (kMaxIpPacketSize
- packet
->size()) / 4);
526 MissingFramesAndPacketsMap::const_iterator frame_it
=
527 cast
->missing_frames_and_packets_
.begin();
529 for (; frame_it
!= cast
->missing_frames_and_packets_
.end() &&
530 number_of_loss_fields
< max_number_of_loss_fields
;
532 // Iterate through all frames with missing packets.
533 if (frame_it
->second
.empty()) {
534 // Special case all packets in a frame is missing.
535 start_size
= packet
->size();
536 packet
->resize(start_size
+ 4);
537 net::BigEndianWriter
big_endian_nack_writer(&((*packet
)[start_size
]), 4);
538 big_endian_nack_writer
.WriteU8(static_cast<uint8
>(frame_it
->first
));
539 big_endian_nack_writer
.WriteU16(kRtcpCastAllPacketsLost
);
540 big_endian_nack_writer
.WriteU8(0);
541 ++number_of_loss_fields
;
543 PacketIdSet::const_iterator packet_it
= frame_it
->second
.begin();
544 while (packet_it
!= frame_it
->second
.end()) {
545 uint16 packet_id
= *packet_it
;
547 start_size
= packet
->size();
548 packet
->resize(start_size
+ 4);
549 net::BigEndianWriter
big_endian_nack_writer(&((*packet
)[start_size
]),
552 // Write frame and packet id to buffer before calculating bitmask.
553 big_endian_nack_writer
.WriteU8(static_cast<uint8
>(frame_it
->first
));
554 big_endian_nack_writer
.WriteU16(packet_id
);
558 while (packet_it
!= frame_it
->second
.end()) {
559 int shift
= static_cast<uint8
>(*packet_it
- packet_id
) - 1;
560 if (shift
>= 0 && shift
<= 7) {
561 bitmask
|= (1 << shift
);
567 big_endian_nack_writer
.WriteU8(bitmask
);
568 ++number_of_loss_fields
;
572 DCHECK_LE(number_of_loss_fields
, kRtcpMaxCastLossFields
);
573 (*packet
)[cast_size_pos
] = static_cast<uint8
>(4 + number_of_loss_fields
);
574 (*packet
)[cast_loss_field_pos
] = static_cast<uint8
>(number_of_loss_fields
);
577 void RtcpSender::BuildReceiverLog(RtcpReceiverLogMessage
* receiver_log_message
,
578 Packet
* packet
) const {
579 DCHECK(receiver_log_message
);
580 const size_t packet_start_size
= packet
->size();
581 size_t number_of_frames
= 0;
582 size_t total_number_of_messages_to_send
= 0;
583 size_t rtcp_log_size
= 0;
585 if (!ScanRtcpReceiverLogMessage(
586 *receiver_log_message
, packet_start_size
, &number_of_frames
,
587 &total_number_of_messages_to_send
, &rtcp_log_size
)) {
590 packet
->resize(packet_start_size
+ rtcp_log_size
);
592 net::BigEndianWriter
big_endian_writer(&((*packet
)[packet_start_size
]),
594 big_endian_writer
.WriteU8(0x80 + kReceiverLogSubtype
);
595 big_endian_writer
.WriteU8(transport::kPacketTypeApplicationDefined
);
596 big_endian_writer
.WriteU16(static_cast<uint16
>(
597 2 + 2 * number_of_frames
+ total_number_of_messages_to_send
));
598 big_endian_writer
.WriteU32(ssrc_
); // Add our own SSRC.
599 big_endian_writer
.WriteU32(kCast
);
601 while (!receiver_log_message
->empty() &&
602 total_number_of_messages_to_send
> 0) {
603 RtcpReceiverFrameLogMessage
& frame_log_messages(
604 receiver_log_message
->front());
606 // Add our frame header.
607 big_endian_writer
.WriteU32(frame_log_messages
.rtp_timestamp_
);
608 size_t messages_in_frame
= frame_log_messages
.event_log_messages_
.size();
609 if (messages_in_frame
> total_number_of_messages_to_send
) {
610 // We are running out of space.
611 messages_in_frame
= total_number_of_messages_to_send
;
613 // Keep track of how many messages we have left to send.
614 total_number_of_messages_to_send
-= messages_in_frame
;
616 // On the wire format is number of messages - 1.
617 big_endian_writer
.WriteU8(static_cast<uint8
>(messages_in_frame
- 1));
619 base::TimeTicks event_timestamp_base
=
620 frame_log_messages
.event_log_messages_
.front().event_timestamp
;
621 uint32 base_timestamp_ms
=
622 (event_timestamp_base
- base::TimeTicks()).InMilliseconds();
623 big_endian_writer
.WriteU8(static_cast<uint8
>(base_timestamp_ms
>> 16));
624 big_endian_writer
.WriteU8(static_cast<uint8
>(base_timestamp_ms
>> 8));
625 big_endian_writer
.WriteU8(static_cast<uint8
>(base_timestamp_ms
));
627 while (!frame_log_messages
.event_log_messages_
.empty() &&
628 messages_in_frame
> 0) {
629 const RtcpReceiverEventLogMessage
& event_message
=
630 frame_log_messages
.event_log_messages_
.front();
631 uint16 event_type_and_timestamp_delta
=
632 MergeEventTypeAndTimestampForWireFormat(
634 event_message
.event_timestamp
- event_timestamp_base
);
635 switch (event_message
.type
) {
638 case kAudioPlayoutDelay
:
639 case kAudioFrameDecoded
:
640 case kVideoFrameDecoded
:
641 case kVideoRenderDelay
:
642 big_endian_writer
.WriteU16(
643 static_cast<uint16
>(event_message
.delay_delta
.InMilliseconds()));
644 big_endian_writer
.WriteU16(event_type_and_timestamp_delta
);
646 case kAudioPacketReceived
:
647 case kVideoPacketReceived
:
648 case kDuplicatePacketReceived
:
649 big_endian_writer
.WriteU16(event_message
.packet_id
);
650 big_endian_writer
.WriteU16(event_type_and_timestamp_delta
);
656 frame_log_messages
.event_log_messages_
.pop_front();
658 if (frame_log_messages
.event_log_messages_
.empty()) {
659 // We sent all messages on this frame; pop the frame header.
660 receiver_log_message
->pop_front();
663 DCHECK_EQ(total_number_of_messages_to_send
, 0);