Home | History | Annotate | Download | only in rtp
      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.
      4 
      5 #include "media/cast/net/rtp/rtp_packet_builder.h"
      6 
      7 #include "base/big_endian.h"
      8 #include "base/logging.h"
      9 
     10 namespace media {
     11 namespace cast {
     12 
     13 const uint32 kCastRtpHeaderLength = 7;
     14 const uint32 kGenericRtpHeaderLength = 12;
     15 static const uint8 kRtpMarkerBitMask = 0x80;
     16 
     17 RtpPacketBuilder::RtpPacketBuilder()
     18     : is_key_(false),
     19       frame_id_(0),
     20       packet_id_(0),
     21       max_packet_id_(0),
     22       reference_frame_id_(0),
     23       timestamp_(0),
     24       sequence_number_(0),
     25       marker_(false),
     26       payload_type_(0),
     27       ssrc_(0) {}
     28 
     29 void RtpPacketBuilder::SetKeyFrame(bool is_key) { is_key_ = is_key; }
     30 
     31 void RtpPacketBuilder::SetFrameIds(uint32 frame_id, uint32 reference_frame_id) {
     32   frame_id_ = frame_id;
     33   reference_frame_id_ = reference_frame_id;
     34 }
     35 
     36 void RtpPacketBuilder::SetPacketId(uint16 packet_id) { packet_id_ = packet_id; }
     37 
     38 void RtpPacketBuilder::SetMaxPacketId(uint16 max_packet_id) {
     39   max_packet_id_ = max_packet_id;
     40 }
     41 
     42 void RtpPacketBuilder::SetTimestamp(uint32 timestamp) {
     43   timestamp_ = timestamp;
     44 }
     45 
     46 void RtpPacketBuilder::SetSequenceNumber(uint16 sequence_number) {
     47   sequence_number_ = sequence_number;
     48 }
     49 
     50 void RtpPacketBuilder::SetMarkerBit(bool marker) { marker_ = marker; }
     51 
     52 void RtpPacketBuilder::SetPayloadType(int payload_type) {
     53   payload_type_ = payload_type;
     54 }
     55 
     56 void RtpPacketBuilder::SetSsrc(uint32 ssrc) { ssrc_ = ssrc; }
     57 
     58 void RtpPacketBuilder::BuildHeader(uint8* data, uint32 data_length) {
     59   BuildCommonHeader(data, data_length);
     60   BuildCastHeader(data + kGenericRtpHeaderLength,
     61                   data_length - kGenericRtpHeaderLength);
     62 }
     63 
     64 void RtpPacketBuilder::BuildCastHeader(uint8* data, uint32 data_length) {
     65   // Build header.
     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_);
     80   }
     81 }
     82 
     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_);
     91 }
     92 
     93 }  // namespace cast
     94 }  // namespace media
     95