Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / rtp / rtp_header_parser.cc
blob2d70f9827dc95b3f3d1a9b977d1169120fa8c0c1
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_header_parser.h"
7 #include <cstddef>
9 #include "base/big_endian.h"
11 namespace media {
12 namespace cast {
14 static const uint8 kCastKeyFrameBitMask = 0x80;
15 static const uint8 kCastReferenceFrameIdBitMask = 0x40;
16 static const size_t kRtpCommonHeaderLength = 12;
17 static const size_t kRtpCastHeaderLength = 12;
19 RtpCastTestHeader::RtpCastTestHeader()
20 : is_key_frame(false),
21 frame_id(0),
22 packet_id(0),
23 max_packet_id(0),
24 is_reference(false),
25 reference_frame_id(0),
26 marker(false),
27 sequence_number(0),
28 rtp_timestamp(0),
29 ssrc(0),
30 payload_type(0),
31 num_csrcs(0),
32 audio_num_energy(0),
33 header_length(0) {}
35 RtpCastTestHeader::~RtpCastTestHeader() {}
37 RtpHeaderParser::RtpHeaderParser(const uint8* rtp_data, size_t rtp_data_length)
38 : rtp_data_begin_(rtp_data), length_(rtp_data_length) {}
40 RtpHeaderParser::~RtpHeaderParser() {}
42 bool RtpHeaderParser::Parse(RtpCastTestHeader* parsed_packet) const {
43 if (length_ < kRtpCommonHeaderLength + kRtpCastHeaderLength)
44 return false;
45 if (!ParseCommon(parsed_packet))
46 return false;
47 return ParseCast(parsed_packet);
50 bool RtpHeaderParser::ParseCommon(RtpCastTestHeader* parsed_packet) const {
51 const uint8 version = rtp_data_begin_[0] >> 6;
52 if (version != 2) {
53 return false;
56 const uint8 num_csrcs = rtp_data_begin_[0] & 0x0f;
57 const bool marker = ((rtp_data_begin_[1] & 0x80) == 0) ? false : true;
58 const uint8 payload_type = rtp_data_begin_[1] & 0x7f;
59 const uint16 sequence_number = (rtp_data_begin_[2] << 8) + rtp_data_begin_[3];
61 const uint8* ptr = &rtp_data_begin_[4];
63 base::BigEndianReader big_endian_reader(reinterpret_cast<const char*>(ptr),
64 8);
65 uint32 rtp_timestamp, ssrc;
66 big_endian_reader.ReadU32(&rtp_timestamp);
67 big_endian_reader.ReadU32(&ssrc);
69 const uint8 csrc_octs = num_csrcs * 4;
71 parsed_packet->marker = marker;
72 parsed_packet->payload_type = payload_type;
73 parsed_packet->sequence_number = sequence_number;
74 parsed_packet->rtp_timestamp = rtp_timestamp;
75 parsed_packet->ssrc = ssrc;
76 parsed_packet->num_csrcs = num_csrcs;
78 parsed_packet->audio_num_energy = parsed_packet->num_csrcs;
80 parsed_packet->header_length = 12 + csrc_octs;
81 return true;
84 bool RtpHeaderParser::ParseCast(RtpCastTestHeader* parsed_packet) const {
85 const uint8* data = rtp_data_begin_ + kRtpCommonHeaderLength;
86 parsed_packet->is_key_frame = (data[0] & kCastKeyFrameBitMask);
87 parsed_packet->is_reference = (data[0] & kCastReferenceFrameIdBitMask);
88 parsed_packet->frame_id = frame_id_wrap_helper_.MapTo32bitsFrameId(data[1]);
90 base::BigEndianReader big_endian_reader(
91 reinterpret_cast<const char*>(data + 2), 8);
92 big_endian_reader.ReadU16(&parsed_packet->packet_id);
93 big_endian_reader.ReadU16(&parsed_packet->max_packet_id);
95 if (parsed_packet->is_reference) {
96 parsed_packet->reference_frame_id =
97 reference_frame_id_wrap_helper_.MapTo32bitsFrameId(data[6]);
99 return true;
102 } // namespace cast
103 } // namespace media