xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_format.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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>(
58           payload, limits, rtp_video_header.frame_type,
59           rtp_video_header.is_last_frame_in_picture);
60     default: {
61       return std::make_unique<RtpPacketizerGeneric>(payload, limits,
62                                                     rtp_video_header);
63     }
64   }
65 }
66 
SplitAboutEqually(int payload_len,const PayloadSizeLimits & limits)67 std::vector<int> RtpPacketizer::SplitAboutEqually(
68     int payload_len,
69     const PayloadSizeLimits& limits) {
70   RTC_DCHECK_GT(payload_len, 0);
71   // First or last packet larger than normal are unsupported.
72   RTC_DCHECK_GE(limits.first_packet_reduction_len, 0);
73   RTC_DCHECK_GE(limits.last_packet_reduction_len, 0);
74 
75   std::vector<int> result;
76   if (limits.max_payload_len >=
77       limits.single_packet_reduction_len + payload_len) {
78     result.push_back(payload_len);
79     return result;
80   }
81   if (limits.max_payload_len - limits.first_packet_reduction_len < 1 ||
82       limits.max_payload_len - limits.last_packet_reduction_len < 1) {
83     // Capacity is not enough to put a single byte into one of the packets.
84     return result;
85   }
86   // First and last packet of the frame can be smaller. Pretend that it's
87   // the same size, but we must write more payload to it.
88   // Assume frame fits in single packet if packet has extra space for sum
89   // of first and last packets reductions.
90   int total_bytes = payload_len + limits.first_packet_reduction_len +
91                     limits.last_packet_reduction_len;
92   // Integer divisions with rounding up.
93   int num_packets_left =
94       (total_bytes + limits.max_payload_len - 1) / limits.max_payload_len;
95   if (num_packets_left == 1) {
96     // Single packet is a special case handled above.
97     num_packets_left = 2;
98   }
99 
100   if (payload_len < num_packets_left) {
101     // Edge case where limits force to have more packets than there are payload
102     // bytes. This may happen when there is single byte of payload that can't be
103     // put into single packet if
104     // first_packet_reduction + last_packet_reduction >= max_payload_len.
105     return result;
106   }
107 
108   int bytes_per_packet = total_bytes / num_packets_left;
109   int num_larger_packets = total_bytes % num_packets_left;
110   int remaining_data = payload_len;
111 
112   result.reserve(num_packets_left);
113   bool first_packet = true;
114   while (remaining_data > 0) {
115     // Last num_larger_packets are 1 byte wider than the rest. Increase
116     // per-packet payload size when needed.
117     if (num_packets_left == num_larger_packets)
118       ++bytes_per_packet;
119     int current_packet_bytes = bytes_per_packet;
120     if (first_packet) {
121       if (current_packet_bytes > limits.first_packet_reduction_len + 1)
122         current_packet_bytes -= limits.first_packet_reduction_len;
123       else
124         current_packet_bytes = 1;
125     }
126     if (current_packet_bytes > remaining_data) {
127       current_packet_bytes = remaining_data;
128     }
129     // This is not the last packet in the whole payload, but there's no data
130     // left for the last packet. Leave at least one byte for the last packet.
131     if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
132       --current_packet_bytes;
133     }
134     result.push_back(current_packet_bytes);
135 
136     remaining_data -= current_packet_bytes;
137     --num_packets_left;
138     first_packet = false;
139   }
140 
141   return result;
142 }
143 
144 }  // namespace webrtc
145