• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include "modules/rtp_rtcp/source/ulpfec_header_reader_writer.h"
12 
13 #include <string.h>
14 
15 #include "api/scoped_refptr.h"
16 #include "modules/rtp_rtcp/source/byte_io.h"
17 #include "modules/rtp_rtcp/source/forward_error_correction_internal.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 
24 // Maximum number of media packets that can be protected in one batch.
25 constexpr size_t kMaxMediaPackets = 48;
26 
27 // Maximum number of FEC packets stored inside ForwardErrorCorrection.
28 constexpr size_t kMaxFecPackets = kMaxMediaPackets;
29 
30 // FEC Level 0 header size in bytes.
31 constexpr size_t kFecLevel0HeaderSize = 10;
32 
33 // FEC Level 1 (ULP) header size in bytes (L bit is set).
34 constexpr size_t kFecLevel1HeaderSizeLBitSet = 2 + kUlpfecPacketMaskSizeLBitSet;
35 
36 // FEC Level 1 (ULP) header size in bytes (L bit is cleared).
37 constexpr size_t kFecLevel1HeaderSizeLBitClear =
38     2 + kUlpfecPacketMaskSizeLBitClear;
39 
40 constexpr size_t kPacketMaskOffset = kFecLevel0HeaderSize + 2;
41 
UlpfecHeaderSize(size_t packet_mask_size)42 size_t UlpfecHeaderSize(size_t packet_mask_size) {
43   RTC_DCHECK_LE(packet_mask_size, kUlpfecPacketMaskSizeLBitSet);
44   if (packet_mask_size <= kUlpfecPacketMaskSizeLBitClear) {
45     return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitClear;
46   } else {
47     return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet;
48   }
49 }
50 
51 }  // namespace
52 
UlpfecHeaderReader()53 UlpfecHeaderReader::UlpfecHeaderReader()
54     : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {}
55 
56 UlpfecHeaderReader::~UlpfecHeaderReader() = default;
57 
ReadFecHeader(ForwardErrorCorrection::ReceivedFecPacket * fec_packet) const58 bool UlpfecHeaderReader::ReadFecHeader(
59     ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const {
60   uint8_t* data = fec_packet->pkt->data.data();
61   if (fec_packet->pkt->data.size() < kPacketMaskOffset) {
62     return false;  // Truncated packet.
63   }
64   bool l_bit = (data[0] & 0x40) != 0u;
65   size_t packet_mask_size =
66       l_bit ? kUlpfecPacketMaskSizeLBitSet : kUlpfecPacketMaskSizeLBitClear;
67   fec_packet->fec_header_size = UlpfecHeaderSize(packet_mask_size);
68   uint16_t seq_num_base = ByteReader<uint16_t>::ReadBigEndian(&data[2]);
69   fec_packet->protected_ssrc = fec_packet->ssrc;  // Due to RED.
70   fec_packet->seq_num_base = seq_num_base;
71   fec_packet->packet_mask_offset = kPacketMaskOffset;
72   fec_packet->packet_mask_size = packet_mask_size;
73   fec_packet->protection_length =
74       ByteReader<uint16_t>::ReadBigEndian(&data[10]);
75 
76   // Store length recovery field in temporary location in header.
77   // This makes the header "compatible" with the corresponding
78   // FlexFEC location of the length recovery field, thus simplifying
79   // the XORing operations.
80   memcpy(&data[2], &data[8], 2);
81 
82   return true;
83 }
84 
UlpfecHeaderWriter()85 UlpfecHeaderWriter::UlpfecHeaderWriter()
86     : FecHeaderWriter(kMaxMediaPackets,
87                       kMaxFecPackets,
88                       kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet) {}
89 
90 UlpfecHeaderWriter::~UlpfecHeaderWriter() = default;
91 
92 // TODO(brandtr): Consider updating this implementation (which actually
93 // returns a bound on the sequence number spread), if logic is added to
94 // UlpfecHeaderWriter::FinalizeFecHeader to truncate packet masks which end
95 // in a string of zeroes. (Similar to how it is done in the FlexFEC case.)
MinPacketMaskSize(const uint8_t * packet_mask,size_t packet_mask_size) const96 size_t UlpfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask,
97                                              size_t packet_mask_size) const {
98   return packet_mask_size;
99 }
100 
FecHeaderSize(size_t packet_mask_size) const101 size_t UlpfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const {
102   return UlpfecHeaderSize(packet_mask_size);
103 }
104 
FinalizeFecHeader(uint32_t,uint16_t seq_num_base,const uint8_t * packet_mask,size_t packet_mask_size,ForwardErrorCorrection::Packet * fec_packet) const105 void UlpfecHeaderWriter::FinalizeFecHeader(
106     uint32_t /* media_ssrc */,
107     uint16_t seq_num_base,
108     const uint8_t* packet_mask,
109     size_t packet_mask_size,
110     ForwardErrorCorrection::Packet* fec_packet) const {
111   uint8_t* data = fec_packet->data.data();
112   // Set E bit to zero.
113   data[0] &= 0x7f;
114   // Set L bit based on packet mask size. (Note that the packet mask
115   // can only take on two discrete values.)
116   bool l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet);
117   if (l_bit) {
118     data[0] |= 0x40;  // Set the L bit.
119   } else {
120     RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear);
121     data[0] &= 0xbf;  // Clear the L bit.
122   }
123   // Copy length recovery field from temporary location.
124   memcpy(&data[8], &data[2], 2);
125   // Write sequence number base.
126   ByteWriter<uint16_t>::WriteBigEndian(&data[2], seq_num_base);
127   // Protection length is set to entire packet. (This is not
128   // required in general.)
129   const size_t fec_header_size = FecHeaderSize(packet_mask_size);
130   ByteWriter<uint16_t>::WriteBigEndian(
131       &data[10], fec_packet->data.size() - fec_header_size);
132   // Copy the packet mask.
133   memcpy(&data[12], packet_mask, packet_mask_size);
134 }
135 
136 }  // namespace webrtc
137