1 /*
2 * Copyright (c) 2014 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/rtp_format.h"
12
13 #include <memory>
14
15 #include "absl/types/variant.h"
16 #include "modules/rtp_rtcp/source/rtp_format_h264.h"
17 #include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
18 #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
19 #include "modules/rtp_rtcp/source/rtp_format_vp9.h"
20 #include "modules/rtp_rtcp/source/rtp_packetizer_av1.h"
21 #include "modules/video_coding/codecs/h264/include/h264_globals.h"
22 #include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
23 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
24 #include "rtc_base/checks.h"
25
26 namespace webrtc {
27
Create(absl::optional<VideoCodecType> type,rtc::ArrayView<const uint8_t> payload,PayloadSizeLimits limits,const RTPVideoHeader & rtp_video_header)28 std::unique_ptr<RtpPacketizer> RtpPacketizer::Create(
29 absl::optional<VideoCodecType> type,
30 rtc::ArrayView<const uint8_t> payload,
31 PayloadSizeLimits limits,
32 // Codec-specific details.
33 const RTPVideoHeader& rtp_video_header) {
34 if (!type) {
35 // Use raw packetizer.
36 return std::make_unique<RtpPacketizerGeneric>(payload, limits);
37 }
38
39 switch (*type) {
40 case kVideoCodecH264: {
41 const auto& h264 =
42 absl::get<RTPVideoHeaderH264>(rtp_video_header.video_type_header);
43 return std::make_unique<RtpPacketizerH264>(payload, limits,
44 h264.packetization_mode);
45 }
46 case kVideoCodecVP8: {
47 const auto& vp8 =
48 absl::get<RTPVideoHeaderVP8>(rtp_video_header.video_type_header);
49 return std::make_unique<RtpPacketizerVp8>(payload, limits, vp8);
50 }
51 case kVideoCodecVP9: {
52 const auto& vp9 =
53 absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
54 return std::make_unique<RtpPacketizerVp9>(payload, limits, vp9);
55 }
56 case kVideoCodecAV1:
57 return std::make_unique<RtpPacketizerAv1>(payload, limits,
58 rtp_video_header.frame_type);
59 default: {
60 return std::make_unique<RtpPacketizerGeneric>(payload, limits,
61 rtp_video_header);
62 }
63 }
64 }
65
SplitAboutEqually(int payload_len,const PayloadSizeLimits & limits)66 std::vector<int> RtpPacketizer::SplitAboutEqually(
67 int payload_len,
68 const PayloadSizeLimits& limits) {
69 RTC_DCHECK_GT(payload_len, 0);
70 // First or last packet larger than normal are unsupported.
71 RTC_DCHECK_GE(limits.first_packet_reduction_len, 0);
72 RTC_DCHECK_GE(limits.last_packet_reduction_len, 0);
73
74 std::vector<int> result;
75 if (limits.max_payload_len >=
76 limits.single_packet_reduction_len + payload_len) {
77 result.push_back(payload_len);
78 return result;
79 }
80 if (limits.max_payload_len - limits.first_packet_reduction_len < 1 ||
81 limits.max_payload_len - limits.last_packet_reduction_len < 1) {
82 // Capacity is not enough to put a single byte into one of the packets.
83 return result;
84 }
85 // First and last packet of the frame can be smaller. Pretend that it's
86 // the same size, but we must write more payload to it.
87 // Assume frame fits in single packet if packet has extra space for sum
88 // of first and last packets reductions.
89 int total_bytes = payload_len + limits.first_packet_reduction_len +
90 limits.last_packet_reduction_len;
91 // Integer divisions with rounding up.
92 int num_packets_left =
93 (total_bytes + limits.max_payload_len - 1) / limits.max_payload_len;
94 if (num_packets_left == 1) {
95 // Single packet is a special case handled above.
96 num_packets_left = 2;
97 }
98
99 if (payload_len < num_packets_left) {
100 // Edge case where limits force to have more packets than there are payload
101 // bytes. This may happen when there is single byte of payload that can't be
102 // put into single packet if
103 // first_packet_reduction + last_packet_reduction >= max_payload_len.
104 return result;
105 }
106
107 int bytes_per_packet = total_bytes / num_packets_left;
108 int num_larger_packets = total_bytes % num_packets_left;
109 int remaining_data = payload_len;
110
111 result.reserve(num_packets_left);
112 bool first_packet = true;
113 while (remaining_data > 0) {
114 // Last num_larger_packets are 1 byte wider than the rest. Increase
115 // per-packet payload size when needed.
116 if (num_packets_left == num_larger_packets)
117 ++bytes_per_packet;
118 int current_packet_bytes = bytes_per_packet;
119 if (first_packet) {
120 if (current_packet_bytes > limits.first_packet_reduction_len + 1)
121 current_packet_bytes -= limits.first_packet_reduction_len;
122 else
123 current_packet_bytes = 1;
124 }
125 if (current_packet_bytes > remaining_data) {
126 current_packet_bytes = remaining_data;
127 }
128 // This is not the last packet in the whole payload, but there's no data
129 // left for the last packet. Leave at least one byte for the last packet.
130 if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
131 --current_packet_bytes;
132 }
133 result.push_back(current_packet_bytes);
134
135 remaining_data -= current_packet_bytes;
136 --num_packets_left;
137 first_packet = false;
138 }
139
140 return result;
141 }
142
143 } // namespace webrtc
144