aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/rtp_rtcp/source/rtcp_packet/sli.cc
blob: 829f3a9db98aa8f6c6e1c8a0c24507c9780242ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sli.h"

#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"

using webrtc::RTCPUtility::RtcpCommonHeader;

namespace webrtc {
namespace rtcp {
// RFC 4585: Feedback format.
//
// Common packet format:
//
//   0                   1                   2                   3
//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |V=2|P|   FMT   |       PT      |          length               |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |                  SSRC of packet sender                        |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |                  SSRC of media source                         |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  :            Feedback Control Information (FCI)                 :
//  :                                                               :
//
// Slice loss indication (SLI) (RFC 4585).
// FCI:
//   0                   1                   2                   3
//   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |            First        |        Number           | PictureID |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Sli::Macroblocks::Macroblocks(uint8_t picture_id,
                              uint16_t first,
                              uint16_t number) {
  RTC_DCHECK_LE(first, 0x1fff);
  RTC_DCHECK_LE(number, 0x1fff);
  RTC_DCHECK_LE(picture_id, 0x3f);
  item_ = (first << 19) | (number << 6) | picture_id;
}

void Sli::Macroblocks::Parse(const uint8_t* buffer) {
  item_ = ByteReader<uint32_t>::ReadBigEndian(buffer);
}

void Sli::Macroblocks::Create(uint8_t* buffer) const {
  ByteWriter<uint32_t>::WriteBigEndian(buffer, item_);
}

bool Sli::Parse(const RtcpCommonHeader& header, const uint8_t* payload) {
  RTC_DCHECK(header.packet_type == kPacketType);
  RTC_DCHECK(header.count_or_format == kFeedbackMessageType);

  if (header.payload_size_bytes <
      kCommonFeedbackLength + Macroblocks::kLength) {
    LOG(LS_WARNING) << "Packet is too small to be a valid SLI packet";
    return false;
  }

  size_t number_of_items =
      (header.payload_size_bytes - kCommonFeedbackLength) /
      Macroblocks::kLength;

  ParseCommonFeedback(payload);
  items_.resize(number_of_items);

  const uint8_t* next_item = payload + kCommonFeedbackLength;
  for (Macroblocks& item : items_) {
    item.Parse(next_item);
    next_item += Macroblocks::kLength;
  }

  return true;
}

bool Sli::Create(uint8_t* packet,
                 size_t* index,
                 size_t max_length,
                 RtcpPacket::PacketReadyCallback* callback) const {
  RTC_DCHECK(!items_.empty());
  while (*index + BlockLength() > max_length) {
    if (!OnBufferFull(packet, index, callback))
      return false;
  }
  CreateHeader(kFeedbackMessageType, kPacketType, HeaderLength(), packet,
               index);
  CreateCommonFeedback(packet + *index);
  *index += kCommonFeedbackLength;
  for (const Macroblocks& item : items_) {
    item.Create(packet + *index);
    *index += Macroblocks::kLength;
  }
  return true;
}

}  // namespace rtcp
}  // namespace webrtc