1 // Copyright 2014 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/net/rtp/rtp_packet_builder.h"
7 #include "base/big_endian.h"
8 #include "base/logging.h"
13 const uint32 kCastRtpHeaderLength
= 7;
14 const uint32 kGenericRtpHeaderLength
= 12;
15 static const uint8 kRtpMarkerBitMask
= 0x80;
17 RtpPacketBuilder::RtpPacketBuilder()
22 reference_frame_id_(0),
29 void RtpPacketBuilder::SetKeyFrame(bool is_key
) { is_key_
= is_key
; }
31 void RtpPacketBuilder::SetFrameIds(uint32 frame_id
, uint32 reference_frame_id
) {
33 reference_frame_id_
= reference_frame_id
;
36 void RtpPacketBuilder::SetPacketId(uint16 packet_id
) { packet_id_
= packet_id
; }
38 void RtpPacketBuilder::SetMaxPacketId(uint16 max_packet_id
) {
39 max_packet_id_
= max_packet_id
;
42 void RtpPacketBuilder::SetTimestamp(uint32 timestamp
) {
43 timestamp_
= timestamp
;
46 void RtpPacketBuilder::SetSequenceNumber(uint16 sequence_number
) {
47 sequence_number_
= sequence_number
;
50 void RtpPacketBuilder::SetMarkerBit(bool marker
) { marker_
= marker
; }
52 void RtpPacketBuilder::SetPayloadType(int payload_type
) {
53 payload_type_
= payload_type
;
56 void RtpPacketBuilder::SetSsrc(uint32 ssrc
) { ssrc_
= ssrc
; }
58 void RtpPacketBuilder::BuildHeader(uint8
* data
, uint32 data_length
) {
59 BuildCommonHeader(data
, data_length
);
60 BuildCastHeader(data
+ kGenericRtpHeaderLength
,
61 data_length
- kGenericRtpHeaderLength
);
64 void RtpPacketBuilder::BuildCastHeader(uint8
* data
, uint32 data_length
) {
66 DCHECK_LE(kCastRtpHeaderLength
, data_length
);
67 // Set the first 7 bytes to 0.
68 memset(data
, 0, kCastRtpHeaderLength
);
69 base::BigEndianWriter
big_endian_writer(reinterpret_cast<char*>(data
), 56);
70 const bool includes_specific_frame_reference
=
71 (is_key_
&& (reference_frame_id_
!= frame_id_
)) ||
72 (!is_key_
&& (reference_frame_id_
!= (frame_id_
- 1)));
73 big_endian_writer
.WriteU8((is_key_
? 0x80 : 0) |
74 (includes_specific_frame_reference
? 0x40 : 0));
75 big_endian_writer
.WriteU8(frame_id_
);
76 big_endian_writer
.WriteU16(packet_id_
);
77 big_endian_writer
.WriteU16(max_packet_id_
);
78 if (includes_specific_frame_reference
) {
79 big_endian_writer
.WriteU8(reference_frame_id_
);
83 void RtpPacketBuilder::BuildCommonHeader(uint8
* data
, uint32 data_length
) {
84 DCHECK_LE(kGenericRtpHeaderLength
, data_length
);
85 base::BigEndianWriter
big_endian_writer(reinterpret_cast<char*>(data
), 96);
86 big_endian_writer
.WriteU8(0x80);
87 big_endian_writer
.WriteU8(payload_type_
| (marker_
? kRtpMarkerBitMask
: 0));
88 big_endian_writer
.WriteU16(sequence_number_
);
89 big_endian_writer
.WriteU32(timestamp_
);
90 big_endian_writer
.WriteU32(ssrc_
);