File: sender_report_builder.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (84 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (11)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/streaming/sender_report_builder.h"

#include "cast/streaming/packet_util.h"
#include "util/osp_logging.h"

namespace openscreen {
namespace cast {

SenderReportBuilder::SenderReportBuilder(RtcpSession* session)
    : session_(session) {
  OSP_DCHECK(session_);
}

SenderReportBuilder::~SenderReportBuilder() = default;

std::pair<absl::Span<uint8_t>, StatusReportId> SenderReportBuilder::BuildPacket(
    const RtcpSenderReport& sender_report,
    absl::Span<uint8_t> buffer) const {
  OSP_CHECK_GE(buffer.size(), kRequiredBufferSize);

  uint8_t* const packet_begin = buffer.data();

  RtcpCommonHeader header;
  header.packet_type = RtcpPacketType::kSenderReport;
  header.payload_size = kRtcpSenderReportSize;
  if (sender_report.report_block) {
    header.with.report_count = 1;
    header.payload_size += kRtcpReportBlockSize;
  } else {
    header.with.report_count = 0;
  }
  header.AppendFields(&buffer);

  AppendField<uint32_t>(session_->sender_ssrc(), &buffer);
  const NtpTimestamp ntp_timestamp =
      session_->ntp_converter().ToNtpTimestamp(sender_report.reference_time);
  AppendField<uint64_t>(ntp_timestamp, &buffer);
  AppendField<uint32_t>(sender_report.rtp_timestamp.lower_32_bits(), &buffer);
  AppendField<uint32_t>(sender_report.send_packet_count, &buffer);
  AppendField<uint32_t>(sender_report.send_octet_count, &buffer);
  if (sender_report.report_block) {
    sender_report.report_block->AppendFields(&buffer);
  }

  uint8_t* const packet_end = buffer.data();
  return std::make_pair(
      absl::Span<uint8_t>(packet_begin, packet_end - packet_begin),
      ToStatusReportId(ntp_timestamp));
}

Clock::time_point SenderReportBuilder::GetRecentReportTime(
    StatusReportId report_id,
    Clock::time_point on_or_before) const {
  // Assumption: The |report_id| is the middle 32 bits of a 64-bit NtpTimestamp.
  static_assert(ToStatusReportId(NtpTimestamp{0x0192a3b4c5d6e7f8}) ==
                    StatusReportId{0xa3b4c5d6},
                "FIXME: ToStatusReportId() implementation changed.");

  // Compute the maximum possible NtpTimestamp. Then, use its uppermost 16 bits
  // and the 32 bits from the report_id to produce a reconstructed NtpTimestamp.
  const NtpTimestamp max_timestamp =
      session_->ntp_converter().ToNtpTimestamp(on_or_before);
  // max_timestamp: HH......
  //     report_id:     LLLL
  //                ↓↓ ↙↙↙↙
  // reconstructed: HHLLLL00
  NtpTimestamp reconstructed = (max_timestamp & (uint64_t{0xffff} << 48)) |
                               (static_cast<uint64_t>(report_id) << 16);
  //  If the reconstructed timestamp is greater than the maximum one, rollover
  //  of the lower 48 bits occurred. Subtract one from the upper 16 bits to
  //  rectify that.
  if (reconstructed > max_timestamp) {
    reconstructed -= uint64_t{1} << 48;
  }

  return session_->ntp_converter().ToLocalTime(reconstructed);
}

}  // namespace cast
}  // namespace openscreen