1 // Copyright 2019 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 "cast/streaming/rtp_packetizer.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <random>
10
11 #include "cast/streaming/packet_util.h"
12 #include "platform/api/time.h"
13 #include "util/big_endian.h"
14 #include "util/integer_division.h"
15 #include "util/osp_logging.h"
16
17 namespace openscreen {
18 namespace cast {
19
20 namespace {
21
22 // Returns a random sequence number to start with. The reason for using a random
23 // number instead of zero is unclear, but this has existed both in several
24 // versions of the Cast Streaming spec and in other implementations for many
25 // years.
GenerateRandomSequenceNumberStart()26 uint16_t GenerateRandomSequenceNumberStart() {
27 // Use a statically-allocated generator, instantiated upon first use, and
28 // seeded with the current time tick count. This generator was chosen because
29 // it is light-weight and does not need to produce unguessable (nor
30 // crypto-secure) values.
31 static std::minstd_rand generator(static_cast<std::minstd_rand::result_type>(
32 Clock::now().time_since_epoch().count()));
33
34 return std::uniform_int_distribution<uint16_t>()(generator);
35 }
36
37 } // namespace
38
RtpPacketizer(RtpPayloadType payload_type,Ssrc sender_ssrc,int max_packet_size)39 RtpPacketizer::RtpPacketizer(RtpPayloadType payload_type,
40 Ssrc sender_ssrc,
41 int max_packet_size)
42 : payload_type_7bits_(static_cast<uint8_t>(payload_type)),
43 sender_ssrc_(sender_ssrc),
44 max_packet_size_(max_packet_size),
45 sequence_number_(GenerateRandomSequenceNumberStart()) {
46 OSP_DCHECK(IsRtpPayloadType(payload_type_7bits_));
47 OSP_DCHECK_GT(max_packet_size_, kMaxRtpHeaderSize);
48 }
49
50 RtpPacketizer::~RtpPacketizer() = default;
51
GeneratePacket(const EncryptedFrame & frame,FramePacketId packet_id,absl::Span<uint8_t> buffer)52 absl::Span<uint8_t> RtpPacketizer::GeneratePacket(const EncryptedFrame& frame,
53 FramePacketId packet_id,
54 absl::Span<uint8_t> buffer) {
55 OSP_CHECK_GE(static_cast<int>(buffer.size()), max_packet_size_);
56
57 const int num_packets = ComputeNumberOfPackets(frame);
58 OSP_DCHECK_GT(num_packets, 0);
59 OSP_DCHECK_LT(int{packet_id}, num_packets);
60 const bool is_last_packet = int{packet_id} == (num_packets - 1);
61
62 // Compute the size of this packet, which is the number of bytes of header
63 // plus the number of bytes of payload. Note that the optional Adaptive
64 // Latency information is only added to the first packet.
65 int packet_size = kBaseRtpHeaderSize;
66 const bool include_adaptive_latency_change =
67 (packet_id == 0 &&
68 frame.new_playout_delay > std::chrono::milliseconds(0));
69 if (include_adaptive_latency_change) {
70 OSP_DCHECK_LE(frame.new_playout_delay.count(),
71 int{std::numeric_limits<uint16_t>::max()});
72 packet_size += kAdaptiveLatencyHeaderSize;
73 }
74 int data_chunk_size = max_payload_size();
75 const int data_chunk_start = data_chunk_size * int{packet_id};
76 if (is_last_packet) {
77 data_chunk_size = static_cast<int>(frame.data.size()) - data_chunk_start;
78 }
79 packet_size += data_chunk_size;
80 OSP_DCHECK_LE(packet_size, max_packet_size_);
81 const absl::Span<uint8_t> packet(buffer.data(), packet_size);
82
83 // RTP Header.
84 AppendField<uint8_t>(kRtpRequiredFirstByte, &buffer);
85 AppendField<uint8_t>(
86 (is_last_packet ? kRtpMarkerBitMask : 0) | payload_type_7bits_, &buffer);
87 AppendField<uint16_t>(sequence_number_++, &buffer);
88 AppendField<uint32_t>(frame.rtp_timestamp.lower_32_bits(), &buffer);
89 AppendField<uint32_t>(sender_ssrc_, &buffer);
90
91 // Cast Header.
92 AppendField<uint8_t>(
93 ((frame.dependency == EncodedFrame::KEY_FRAME) ? kRtpKeyFrameBitMask
94 : 0) |
95 kRtpHasReferenceFrameIdBitMask |
96 (include_adaptive_latency_change ? 1 : 0),
97 &buffer);
98 AppendField<uint8_t>(frame.frame_id.lower_8_bits(), &buffer);
99 AppendField<uint16_t>(packet_id, &buffer);
100 AppendField<uint16_t>(num_packets - 1, &buffer);
101 AppendField<uint8_t>(frame.referenced_frame_id.lower_8_bits(), &buffer);
102
103 // Extension of Cast Header for Adaptive Latency change.
104 if (include_adaptive_latency_change) {
105 AppendField<uint16_t>(
106 (kAdaptiveLatencyRtpExtensionType << kNumExtensionDataSizeFieldBits) |
107 sizeof(uint16_t),
108 &buffer);
109 AppendField<uint16_t>(frame.new_playout_delay.count(), &buffer);
110 }
111
112 // Sanity-check the pointer math, to ensure the packet is being entirely
113 // populated, with no underrun or overrun.
114 OSP_DCHECK_EQ(buffer.data() + data_chunk_size, packet.end());
115
116 // Copy the encrypted payload data into the packet.
117 memcpy(buffer.data(), frame.data.data() + data_chunk_start, data_chunk_size);
118
119 return packet;
120 }
121
ComputeNumberOfPackets(const EncryptedFrame & frame) const122 int RtpPacketizer::ComputeNumberOfPackets(const EncryptedFrame& frame) const {
123 // The total number of packets is computed by assuming the payload will be
124 // split-up across as few packets as possible.
125 int num_packets = DividePositivesRoundingUp(
126 static_cast<int>(frame.data.size()), max_payload_size());
127 // Edge case: There must always be at least one packet, even when there are no
128 // payload bytes. Some audio codecs, for example, use zero bytes to represent
129 // a period of silence.
130 num_packets = std::max(1, num_packets);
131
132 // Ensure that the entire range of FramePacketIds can be represented.
133 return num_packets <= int{kMaxAllowedFramePacketId} ? num_packets : -1;
134 }
135
136 } // namespace cast
137 } // namespace openscreen
138