Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "webrtc/modules/rtp_rtcp/source/ulpfec_header_reader_writer.h" | |
| 12 | |
| 13 #include <utility> | |
| 14 | |
| 15 #include "webrtc/base/checks.h" | |
| 16 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
| 17 #include "webrtc/modules/rtp_rtcp/source/forward_error_correction_internal.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Maximum number of media packets that can be protected in one batch. | |
| 24 constexpr size_t kMaxMediaPackets = 48; | |
| 25 | |
| 26 // Maximum number of FEC packets stored inside ForwardErrorCorrection. | |
| 27 constexpr size_t kMaxFecPackets = kMaxMediaPackets; | |
| 28 | |
| 29 // FEC Level 0 header size in bytes. | |
| 30 constexpr size_t kFecLevel0HeaderSize = 10; | |
| 31 | |
| 32 // FEC Level 1 (ULP) header size in bytes (L bit is set). | |
| 33 constexpr size_t kFecLevel1HeaderSizeLBitSet = | |
| 34 2 + webrtc::kUlpfecPacketMaskSizeLBitSet; | |
|
danilchap
2016/08/24 10:42:40
do you need namespace here?
brandtr
2016/08/24 11:34:24
Nope, that was from before. Thanks!
| |
| 35 | |
| 36 // FEC Level 1 (ULP) header size in bytes (L bit is cleared). | |
| 37 constexpr size_t kFecLevel1HeaderSizeLBitClear = | |
| 38 2 + webrtc::kUlpfecPacketMaskSizeLBitClear; | |
| 39 | |
| 40 size_t FecHeaderSizeImpl(size_t packet_mask_size) { | |
| 41 RTC_DCHECK_LE(packet_mask_size, webrtc::kUlpfecPacketMaskSizeLBitSet); | |
| 42 if (packet_mask_size <= webrtc::kUlpfecPacketMaskSizeLBitClear) { | |
| 43 return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitClear; | |
| 44 } else { | |
| 45 return kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 UlpfecHeaderReader::UlpfecHeaderReader() | |
| 52 : FecHeaderReader(kMaxMediaPackets, kMaxFecPackets) {} | |
| 53 | |
| 54 UlpfecHeaderReader::~UlpfecHeaderReader() = default; | |
| 55 | |
| 56 bool UlpfecHeaderReader::ReadFecHeader( | |
| 57 ForwardErrorCorrection::ReceivedFecPacket* fec_packet) const { | |
| 58 bool l_bit = (fec_packet->pkt->data[0] & 0x40) != 0u; | |
| 59 size_t packet_mask_size = | |
| 60 l_bit ? kUlpfecPacketMaskSizeLBitSet : kUlpfecPacketMaskSizeLBitClear; | |
| 61 fec_packet->fec_header_size = FecHeaderSizeImpl(packet_mask_size); | |
| 62 uint16_t seq_num_base = | |
| 63 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[2]); | |
| 64 const size_t kPacketMaskOffset = kFecLevel0HeaderSize + 2; | |
| 65 fec_packet->packet_mask_info[fec_packet->rtp_ssrc] = | |
| 66 std::make_tuple(seq_num_base, kPacketMaskOffset, packet_mask_size); | |
| 67 fec_packet->protection_length = | |
| 68 ByteReader<uint16_t>::ReadBigEndian(&fec_packet->pkt->data[10]); | |
| 69 | |
| 70 // Store length recovery field in temporary location in header. | |
| 71 // This makes the header "compatible" with the corresponding | |
| 72 // FlexFEC location of the length recovery field, thus simplifying | |
| 73 // the XORing operations. | |
| 74 memcpy(&fec_packet->pkt->data[2], &fec_packet->pkt->data[8], 2); | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 UlpfecHeaderWriter::UlpfecHeaderWriter() | |
| 80 : FecHeaderWriter(kMaxMediaPackets, | |
| 81 kMaxFecPackets, | |
| 82 kFecLevel0HeaderSize + kFecLevel1HeaderSizeLBitSet) {} | |
| 83 | |
| 84 UlpfecHeaderWriter::~UlpfecHeaderWriter() = default; | |
| 85 | |
| 86 // TODO(brandtr): Consider updating this implementation (which actually | |
| 87 // returns a bound on the sequence number spread), if logic is added to | |
| 88 // UlpfecHeaderWriter::FinalizeFecHeader to truncate packet masks which end | |
| 89 // in a string of zeroes. (Similar to how it is done in the FlexFEC case.) | |
| 90 size_t UlpfecHeaderWriter::MinPacketMaskSize(const uint8_t* packet_mask, | |
| 91 size_t packet_mask_size) const { | |
| 92 return packet_mask_size; | |
| 93 } | |
| 94 | |
| 95 size_t UlpfecHeaderWriter::FecHeaderSize(size_t packet_mask_size) const { | |
| 96 return FecHeaderSizeImpl(packet_mask_size); | |
| 97 } | |
| 98 | |
| 99 void UlpfecHeaderWriter::FinalizeFecHeader( | |
| 100 const ForwardErrorCorrection::PacketList& media_packets, | |
| 101 const uint8_t* packet_mask, | |
| 102 size_t packet_mask_size, | |
| 103 ForwardErrorCorrection::Packet* fec_packet) const { | |
| 104 // Set E bit to zero. | |
| 105 fec_packet->data[0] &= 0x7f; | |
| 106 // Set L bit based on packet mask size. (Note that the packet mask | |
| 107 // can only take on two discrete values.) | |
| 108 bool l_bit = (packet_mask_size == kUlpfecPacketMaskSizeLBitSet); | |
| 109 if (l_bit) { | |
| 110 fec_packet->data[0] |= 0x40; // Set the L bit. | |
| 111 } else { | |
| 112 RTC_DCHECK_EQ(packet_mask_size, kUlpfecPacketMaskSizeLBitClear); | |
| 113 fec_packet->data[0] &= 0xbf; // Clear the L bit. | |
| 114 } | |
| 115 // Copy length recovery field from temporary location. | |
| 116 memcpy(&fec_packet->data[8], &fec_packet->data[2], 2); | |
| 117 // Sequence number from first media packet used as SN base | |
| 118 // for all FEC packets. (This is not required in general.) | |
| 119 RTC_DCHECK(!media_packets.empty()); | |
| 120 ForwardErrorCorrection::Packet* first_media_packet = | |
| 121 media_packets.front().get(); | |
| 122 RTC_DCHECK(first_media_packet); | |
| 123 uint16_t seq_num_base = | |
| 124 ForwardErrorCorrection::ParseSequenceNumber(first_media_packet->data); | |
| 125 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[2], seq_num_base); | |
| 126 // Protection length is set to entire packet. (This is not | |
| 127 // required in general.) | |
| 128 const size_t fec_header_size = FecHeaderSize(packet_mask_size); | |
| 129 ByteWriter<uint16_t>::WriteBigEndian(&fec_packet->data[10], | |
| 130 fec_packet->length - fec_header_size); | |
| 131 // Copy the packet mask. | |
| 132 memcpy(&fec_packet->data[12], packet_mask, packet_mask_size); | |
| 133 } | |
| 134 | |
| 135 } // namespace webrtc | |
| OLD | NEW |