blob: e870232a4d51e152df60e70063c403b14b2d3da6 [file] [log] [blame]
stefan@webrtc.org2ec56062014-07-31 14:59:24 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtp_format.h"
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000012
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020013#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020014#include "absl/types/variant.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/source/rtp_format_h264.h"
16#include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
17#include "modules/rtp_rtcp/source/rtp_format_vp8.h"
18#include "modules/rtp_rtcp/source/rtp_format_vp9.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "modules/video_coding/codecs/h264/include/h264_globals.h"
20#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
21#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
Danil Chapovalov376e1142018-09-04 16:11:58 +020022#include "rtc_base/checks.h"
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000023
24namespace webrtc {
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020025
26std::unique_ptr<RtpPacketizer> RtpPacketizer::Create(
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +020027 absl::optional<VideoCodecType> type,
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020028 rtc::ArrayView<const uint8_t> payload,
29 PayloadSizeLimits limits,
30 // Codec-specific details.
31 const RTPVideoHeader& rtp_video_header,
Niels Möller87e2d782019-03-07 10:18:23 +010032 VideoFrameType frame_type,
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020033 const RTPFragmentationHeader* fragmentation) {
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +020034 if (!type) {
35 // Use raw packetizer.
36 return absl::make_unique<RtpPacketizerGeneric>(payload, limits);
37 }
38
39 switch (*type) {
philipel7d745e52018-08-02 14:03:53 +020040 case kVideoCodecH264: {
Danil Chapovalovfd5fbd02018-09-12 10:23:15 +020041 RTC_CHECK(fragmentation);
philipel7d745e52018-08-02 14:03:53 +020042 const auto& h264 =
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020043 absl::get<RTPVideoHeaderH264>(rtp_video_header.video_type_header);
Danil Chapovalovfd5fbd02018-09-12 10:23:15 +020044 return absl::make_unique<RtpPacketizerH264>(
45 payload, limits, h264.packetization_mode, *fragmentation);
philipel7d745e52018-08-02 14:03:53 +020046 }
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020047 case kVideoCodecVP8: {
48 const auto& vp8 =
49 absl::get<RTPVideoHeaderVP8>(rtp_video_header.video_type_header);
Danil Chapovalov8d1b5822018-08-30 11:14:05 +020050 return absl::make_unique<RtpPacketizerVp8>(payload, limits, vp8);
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020051 }
philipel29d88462018-08-08 14:26:00 +020052 case kVideoCodecVP9: {
53 const auto& vp9 =
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020054 absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
Danil Chapovalov0b445c62018-09-07 18:33:35 +020055 return absl::make_unique<RtpPacketizerVp9>(payload, limits, vp9);
philipel29d88462018-08-08 14:26:00 +020056 }
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020057 default: {
Danil Chapovalovaf8c0362018-09-05 16:54:22 +020058 return absl::make_unique<RtpPacketizerGeneric>(
59 payload, limits, rtp_video_header, frame_type);
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020060 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000061 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000062}
63
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020064std::vector<int> RtpPacketizer::SplitAboutEqually(
65 int payload_len,
Danil Chapovalov376e1142018-09-04 16:11:58 +020066 const PayloadSizeLimits& limits) {
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020067 RTC_DCHECK_GT(payload_len, 0);
68 // First or last packet larger than normal are unsupported.
69 RTC_DCHECK_GE(limits.first_packet_reduction_len, 0);
70 RTC_DCHECK_GE(limits.last_packet_reduction_len, 0);
Danil Chapovalov376e1142018-09-04 16:11:58 +020071
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020072 std::vector<int> result;
Danil Chapovalovfcebe0e2018-10-12 17:51:22 +020073 if (limits.max_payload_len >=
74 limits.single_packet_reduction_len + payload_len) {
75 result.push_back(payload_len);
76 return result;
77 }
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020078 if (limits.max_payload_len - limits.first_packet_reduction_len < 1 ||
79 limits.max_payload_len - limits.last_packet_reduction_len < 1) {
80 // Capacity is not enough to put a single byte into one of the packets.
81 return result;
82 }
Danil Chapovalovbace3a42018-09-05 16:15:08 +020083 // First and last packet of the frame can be smaller. Pretend that it's
84 // the same size, but we must write more payload to it.
85 // Assume frame fits in single packet if packet has extra space for sum
86 // of first and last packets reductions.
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020087 int total_bytes = payload_len + limits.first_packet_reduction_len +
88 limits.last_packet_reduction_len;
Danil Chapovalov376e1142018-09-04 16:11:58 +020089 // Integer divisions with rounding up.
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020090 int num_packets_left =
Danil Chapovalov376e1142018-09-04 16:11:58 +020091 (total_bytes + limits.max_payload_len - 1) / limits.max_payload_len;
Danil Chapovalovfcebe0e2018-10-12 17:51:22 +020092 if (num_packets_left == 1) {
93 // Single packet is a special case handled above.
94 num_packets_left = 2;
95 }
Danil Chapovalov376e1142018-09-04 16:11:58 +020096
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020097 if (payload_len < num_packets_left) {
98 // Edge case where limits force to have more packets than there are payload
99 // bytes. This may happen when there is single byte of payload that can't be
100 // put into single packet if
101 // first_packet_reduction + last_packet_reduction >= max_payload_len.
102 return result;
103 }
104
105 int bytes_per_packet = total_bytes / num_packets_left;
106 int num_larger_packets = total_bytes % num_packets_left;
107 int remaining_data = payload_len;
108
Danil Chapovalov376e1142018-09-04 16:11:58 +0200109 result.reserve(num_packets_left);
Danil Chapovalovbace3a42018-09-05 16:15:08 +0200110 bool first_packet = true;
Danil Chapovalov376e1142018-09-04 16:11:58 +0200111 while (remaining_data > 0) {
112 // Last num_larger_packets are 1 byte wider than the rest. Increase
113 // per-packet payload size when needed.
114 if (num_packets_left == num_larger_packets)
115 ++bytes_per_packet;
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +0200116 int current_packet_bytes = bytes_per_packet;
Danil Chapovalovbace3a42018-09-05 16:15:08 +0200117 if (first_packet) {
118 if (current_packet_bytes > limits.first_packet_reduction_len + 1)
119 current_packet_bytes -= limits.first_packet_reduction_len;
120 else
121 current_packet_bytes = 1;
122 }
Danil Chapovalov376e1142018-09-04 16:11:58 +0200123 if (current_packet_bytes > remaining_data) {
124 current_packet_bytes = remaining_data;
125 }
126 // This is not the last packet in the whole payload, but there's no data
127 // left for the last packet. Leave at least one byte for the last packet.
128 if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
129 --current_packet_bytes;
130 }
Danil Chapovalov376e1142018-09-04 16:11:58 +0200131 result.push_back(current_packet_bytes);
132
133 remaining_data -= current_packet_bytes;
134 --num_packets_left;
Danil Chapovalovbace3a42018-09-05 16:15:08 +0200135 first_packet = false;
Danil Chapovalov376e1142018-09-04 16:11:58 +0200136 }
137
138 return result;
139}
140
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200141RtpDepacketizer* RtpDepacketizer::Create(absl::optional<VideoCodecType> type) {
142 if (!type) {
143 // Use raw depacketizer.
144 return new RtpDepacketizerGeneric(/*generic_header_enabled=*/false);
145 }
146
147 switch (*type) {
Niels Möller520ca4e2018-06-04 11:14:38 +0200148 case kVideoCodecH264:
pbos@webrtc.org730d2702014-09-29 08:00:22 +0000149 return new RtpDepacketizerH264();
Niels Möller520ca4e2018-06-04 11:14:38 +0200150 case kVideoCodecVP8:
pbos@webrtc.org730d2702014-09-29 08:00:22 +0000151 return new RtpDepacketizerVp8();
Niels Möller520ca4e2018-06-04 11:14:38 +0200152 case kVideoCodecVP9:
asaperssonf38ea3c2015-07-28 04:02:54 -0700153 return new RtpDepacketizerVp9();
Niels Moller1788dcb2018-08-09 06:18:57 +0000154 default:
Mirta Dvornicica24d9342019-05-16 15:51:15 +0200155 return new RtpDepacketizerGeneric(/*generic_header_enabled=*/true);
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000156 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000157}
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200158
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000159} // namespace webrtc