blob: 13ec0afe9f7bd675ac9841be41cb998b9ef151b0 [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
hta9aa96882016-12-06 05:36:03 -080013#include <utility>
14
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#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"
Danil Chapovalov376e1142018-09-04 16:11:58 +020020#include "rtc_base/checks.h"
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000021
22namespace webrtc {
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020023
24std::unique_ptr<RtpPacketizer> RtpPacketizer::Create(
25 VideoCodecType type,
26 rtc::ArrayView<const uint8_t> payload,
27 PayloadSizeLimits limits,
28 // Codec-specific details.
29 const RTPVideoHeader& rtp_video_header,
30 FrameType frame_type,
31 const RTPFragmentationHeader* fragmentation) {
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000032 switch (type) {
philipel7d745e52018-08-02 14:03:53 +020033 case kVideoCodecH264: {
Danil Chapovalovfd5fbd02018-09-12 10:23:15 +020034 RTC_CHECK(fragmentation);
philipel7d745e52018-08-02 14:03:53 +020035 const auto& h264 =
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020036 absl::get<RTPVideoHeaderH264>(rtp_video_header.video_type_header);
Danil Chapovalovfd5fbd02018-09-12 10:23:15 +020037 return absl::make_unique<RtpPacketizerH264>(
38 payload, limits, h264.packetization_mode, *fragmentation);
philipel7d745e52018-08-02 14:03:53 +020039 }
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020040 case kVideoCodecVP8: {
41 const auto& vp8 =
42 absl::get<RTPVideoHeaderVP8>(rtp_video_header.video_type_header);
Danil Chapovalov8d1b5822018-08-30 11:14:05 +020043 return absl::make_unique<RtpPacketizerVp8>(payload, limits, vp8);
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020044 }
philipel29d88462018-08-08 14:26:00 +020045 case kVideoCodecVP9: {
46 const auto& vp9 =
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020047 absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
Danil Chapovalov0b445c62018-09-07 18:33:35 +020048 return absl::make_unique<RtpPacketizerVp9>(payload, limits, vp9);
philipel29d88462018-08-08 14:26:00 +020049 }
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020050 default: {
Danil Chapovalovaf8c0362018-09-05 16:54:22 +020051 return absl::make_unique<RtpPacketizerGeneric>(
52 payload, limits, rtp_video_header, frame_type);
Danil Chapovalovf7f8a1f2018-08-28 19:45:31 +020053 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000054 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000055}
56
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020057std::vector<int> RtpPacketizer::SplitAboutEqually(
58 int payload_len,
Danil Chapovalov376e1142018-09-04 16:11:58 +020059 const PayloadSizeLimits& limits) {
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020060 RTC_DCHECK_GT(payload_len, 0);
61 // First or last packet larger than normal are unsupported.
62 RTC_DCHECK_GE(limits.first_packet_reduction_len, 0);
63 RTC_DCHECK_GE(limits.last_packet_reduction_len, 0);
Danil Chapovalov376e1142018-09-04 16:11:58 +020064
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020065 std::vector<int> result;
66 if (limits.max_payload_len - limits.first_packet_reduction_len < 1 ||
67 limits.max_payload_len - limits.last_packet_reduction_len < 1) {
68 // Capacity is not enough to put a single byte into one of the packets.
69 return result;
70 }
Danil Chapovalovbace3a42018-09-05 16:15:08 +020071 // First and last packet of the frame can be smaller. Pretend that it's
72 // the same size, but we must write more payload to it.
73 // Assume frame fits in single packet if packet has extra space for sum
74 // of first and last packets reductions.
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020075 int total_bytes = payload_len + limits.first_packet_reduction_len +
76 limits.last_packet_reduction_len;
Danil Chapovalov376e1142018-09-04 16:11:58 +020077 // Integer divisions with rounding up.
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020078 int num_packets_left =
Danil Chapovalov376e1142018-09-04 16:11:58 +020079 (total_bytes + limits.max_payload_len - 1) / limits.max_payload_len;
Danil Chapovalov376e1142018-09-04 16:11:58 +020080
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +020081 if (payload_len < num_packets_left) {
82 // Edge case where limits force to have more packets than there are payload
83 // bytes. This may happen when there is single byte of payload that can't be
84 // put into single packet if
85 // first_packet_reduction + last_packet_reduction >= max_payload_len.
86 return result;
87 }
88
89 int bytes_per_packet = total_bytes / num_packets_left;
90 int num_larger_packets = total_bytes % num_packets_left;
91 int remaining_data = payload_len;
92
Danil Chapovalov376e1142018-09-04 16:11:58 +020093 result.reserve(num_packets_left);
Danil Chapovalovbace3a42018-09-05 16:15:08 +020094 bool first_packet = true;
Danil Chapovalov376e1142018-09-04 16:11:58 +020095 while (remaining_data > 0) {
96 // Last num_larger_packets are 1 byte wider than the rest. Increase
97 // per-packet payload size when needed.
98 if (num_packets_left == num_larger_packets)
99 ++bytes_per_packet;
Danil Chapovalovfa5ec8d2018-09-07 10:57:26 +0200100 int current_packet_bytes = bytes_per_packet;
Danil Chapovalovbace3a42018-09-05 16:15:08 +0200101 if (first_packet) {
102 if (current_packet_bytes > limits.first_packet_reduction_len + 1)
103 current_packet_bytes -= limits.first_packet_reduction_len;
104 else
105 current_packet_bytes = 1;
106 }
Danil Chapovalov376e1142018-09-04 16:11:58 +0200107 if (current_packet_bytes > remaining_data) {
108 current_packet_bytes = remaining_data;
109 }
110 // This is not the last packet in the whole payload, but there's no data
111 // left for the last packet. Leave at least one byte for the last packet.
112 if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
113 --current_packet_bytes;
114 }
Danil Chapovalov376e1142018-09-04 16:11:58 +0200115 result.push_back(current_packet_bytes);
116
117 remaining_data -= current_packet_bytes;
118 --num_packets_left;
Danil Chapovalovbace3a42018-09-05 16:15:08 +0200119 first_packet = false;
Danil Chapovalov376e1142018-09-04 16:11:58 +0200120 }
121
122 return result;
123}
124
Niels Möller520ca4e2018-06-04 11:14:38 +0200125RtpDepacketizer* RtpDepacketizer::Create(VideoCodecType type) {
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000126 switch (type) {
Niels Möller520ca4e2018-06-04 11:14:38 +0200127 case kVideoCodecH264:
pbos@webrtc.org730d2702014-09-29 08:00:22 +0000128 return new RtpDepacketizerH264();
Niels Möller520ca4e2018-06-04 11:14:38 +0200129 case kVideoCodecVP8:
pbos@webrtc.org730d2702014-09-29 08:00:22 +0000130 return new RtpDepacketizerVp8();
Niels Möller520ca4e2018-06-04 11:14:38 +0200131 case kVideoCodecVP9:
asaperssonf38ea3c2015-07-28 04:02:54 -0700132 return new RtpDepacketizerVp9();
Niels Moller1788dcb2018-08-09 06:18:57 +0000133 default:
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200134 return new RtpDepacketizerGeneric();
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000135 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000136}
137} // namespace webrtc