OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "webrtc/base/logging.h" | 13 #include "webrtc/base/logging.h" |
14 #include "webrtc/modules/include/module_common_types.h" | 14 #include "webrtc/modules/include/module_common_types.h" |
15 #include "webrtc/modules/rtp_rtcp/source/rtp_format_video_generic.h" | 15 #include "webrtc/modules/rtp_rtcp/source/rtp_format_video_generic.h" |
16 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h" | 16 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 static const size_t kGenericHeaderLength = 1; | 20 static const size_t kGenericHeaderLength = 1; |
21 | 21 |
22 RtpPacketizerGeneric::RtpPacketizerGeneric(FrameType frame_type, | 22 RtpPacketizerGeneric::RtpPacketizerGeneric(FrameType frame_type, |
23 size_t max_payload_len) | 23 size_t max_payload_len, |
24 size_t last_packet_reduction_len) | |
24 : payload_data_(NULL), | 25 : payload_data_(NULL), |
25 payload_size_(0), | 26 payload_size_(0), |
26 max_payload_len_(max_payload_len - kGenericHeaderLength), | 27 max_payload_len_(max_payload_len - kGenericHeaderLength), |
27 frame_type_(frame_type) { | 28 last_packet_reduction_len_(last_packet_reduction_len), |
28 } | 29 frame_type_(frame_type), |
30 num_packets_left_(0), | |
31 num_smaller_packets_(0) {} | |
29 | 32 |
30 RtpPacketizerGeneric::~RtpPacketizerGeneric() { | 33 RtpPacketizerGeneric::~RtpPacketizerGeneric() { |
31 } | 34 } |
32 | 35 |
33 void RtpPacketizerGeneric::SetPayloadData( | 36 size_t RtpPacketizerGeneric::SetPayloadData( |
34 const uint8_t* payload_data, | 37 const uint8_t* payload_data, |
35 size_t payload_size, | 38 size_t payload_size, |
36 const RTPFragmentationHeader* fragmentation) { | 39 const RTPFragmentationHeader* fragmentation) { |
37 payload_data_ = payload_data; | 40 payload_data_ = payload_data; |
38 payload_size_ = payload_size; | 41 payload_size_ = payload_size; |
39 | 42 |
40 // Fragment packets more evenly by splitting the payload up evenly. | 43 // Fragment packets such that they are almost the same size, even accounting |
41 size_t num_packets = | 44 // for larger header in the last packet. |
42 (payload_size_ + max_payload_len_ - 1) / max_payload_len_; | 45 // Since we are given how much extra space is occupied by the longer header |
43 payload_length_ = (payload_size_ + num_packets - 1) / num_packets; | 46 // in the last packet, we can pretend that RTP headers are the same, but |
44 assert(payload_length_ <= max_payload_len_); | 47 // there's last_packet_reduction_len_ virtual payload, to be put at the end of |
48 // the last packet. | |
49 // | |
50 size_t total_bytes = payload_size_ + last_packet_reduction_len_; | |
51 | |
52 // Minimum needed number of packets to fit payload and virtual payload in the | |
53 // last packet. | |
54 num_packets_left_ = (total_bytes + max_payload_len_ - 1) / max_payload_len_; | |
55 // Given number of packets we will use, calculate average size rounded up. | |
56 payload_len_per_packet_ = | |
57 (total_bytes + num_packets_left_ - 1) / num_packets_left_; | |
58 // If we can't divide everything perfectly evenly, we put 1 extra byte in some | |
59 // first packets: 14 bytes in 4 packets would be splitted as 4+4+3+3. | |
60 // There are exactly total_data % num_packets larger packets. | |
61 num_smaller_packets_ = num_packets_left_ - total_bytes % num_packets_left_; | |
62 // If all packets are the same size, we already have correct per packet | |
63 // length. | |
64 if (num_smaller_packets_ == num_packets_left_) | |
65 num_smaller_packets_ = 0; | |
sprang_webrtc
2017/05/17 13:10:07
Same as commented for h264, num_larger_packets_lef
ilnik
2017/05/17 15:06:33
Done.
| |
66 RTC_DCHECK_LE(payload_len_per_packet_, max_payload_len_); | |
45 | 67 |
46 generic_header_ = RtpFormatVideoGeneric::kFirstPacketBit; | 68 generic_header_ = RtpFormatVideoGeneric::kFirstPacketBit; |
47 } | |
48 | |
49 bool RtpPacketizerGeneric::NextPacket(RtpPacketToSend* packet, | |
50 bool* last_packet) { | |
51 RTC_DCHECK(packet); | |
52 RTC_DCHECK(last_packet); | |
53 if (payload_size_ < payload_length_) { | |
54 payload_length_ = payload_size_; | |
55 } | |
56 | |
57 payload_size_ -= payload_length_; | |
58 RTC_DCHECK_LE(payload_length_, max_payload_len_); | |
59 | |
60 uint8_t* out_ptr = | |
61 packet->AllocatePayload(kGenericHeaderLength + payload_length_); | |
62 // Put generic header in packet | |
63 if (frame_type_ == kVideoFrameKey) { | 69 if (frame_type_ == kVideoFrameKey) { |
64 generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit; | 70 generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit; |
65 } | 71 } |
72 return num_packets_left_; | |
73 } | |
74 | |
75 bool RtpPacketizerGeneric::NextPacket(RtpPacketToSend* packet) { | |
76 RTC_DCHECK(packet); | |
77 if (num_packets_left_ == 0) | |
78 return false; | |
79 // Last smaller_packets_ packets are 1 byte smaller than previous packets. | |
80 // Reduce per packet payload once needed. | |
81 if (num_packets_left_ == num_smaller_packets_) | |
82 payload_len_per_packet_--; | |
83 size_t next_packet_payload_len = payload_len_per_packet_; | |
84 if (payload_size_ <= next_packet_payload_len) { | |
85 // Whole payload fits into this packet | |
sprang_webrtc
2017/05/17 13:10:07
nit: end comment with period.
ilnik
2017/05/17 15:06:33
Done.
| |
86 next_packet_payload_len = payload_size_; | |
87 if (num_packets_left_ == 2) { | |
88 // This is the pre-last packet. Leave at least 1 payload byte for the | |
sprang_webrtc
2017/05/17 13:10:07
nit: s/pre-last/penultimate
ilnik
2017/05/17 15:06:33
Done.
| |
89 // last packet. | |
90 next_packet_payload_len--; | |
91 RTC_DCHECK_GT(next_packet_payload_len, 0); | |
92 } | |
93 } | |
94 RTC_DCHECK_LE(next_packet_payload_len, max_payload_len_); | |
95 | |
96 uint8_t* out_ptr = | |
97 packet->AllocatePayload(kGenericHeaderLength + next_packet_payload_len); | |
98 // Put generic header in packet. | |
66 out_ptr[0] = generic_header_; | 99 out_ptr[0] = generic_header_; |
67 // Remove first-packet bit, following packets are intermediate | 100 // Remove first-packet bit, following packets are intermediate. |
68 generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit; | 101 generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit; |
69 | 102 |
70 // Put payload in packet | 103 // Put payload in packet. |
71 memcpy(out_ptr + kGenericHeaderLength, payload_data_, payload_length_); | 104 memcpy(out_ptr + kGenericHeaderLength, payload_data_, |
72 payload_data_ += payload_length_; | 105 next_packet_payload_len); |
106 payload_data_ += next_packet_payload_len; | |
107 payload_size_ -= next_packet_payload_len; | |
108 num_packets_left_--; | |
109 // Packets left to produce and data left to split should end at the same time. | |
110 RTC_DCHECK_EQ(num_packets_left_ == 0, payload_size_ == 0); | |
73 | 111 |
74 *last_packet = payload_size_ <= 0; | 112 packet->SetMarker(payload_size_ == 0); |
75 packet->SetMarker(*last_packet); | 113 |
76 return true; | 114 return true; |
77 } | 115 } |
78 | 116 |
79 ProtectionType RtpPacketizerGeneric::GetProtectionType() { | 117 ProtectionType RtpPacketizerGeneric::GetProtectionType() { |
80 return kProtectedPacket; | 118 return kProtectedPacket; |
81 } | 119 } |
82 | 120 |
83 StorageType RtpPacketizerGeneric::GetStorageType( | 121 StorageType RtpPacketizerGeneric::GetStorageType( |
84 uint32_t retransmission_settings) { | 122 uint32_t retransmission_settings) { |
85 return kAllowRetransmission; | 123 return kAllowRetransmission; |
(...skipping 23 matching lines...) Expand all Loading... | |
109 (generic_header & RtpFormatVideoGeneric::kFirstPacketBit) != 0; | 147 (generic_header & RtpFormatVideoGeneric::kFirstPacketBit) != 0; |
110 parsed_payload->type.Video.codec = kRtpVideoGeneric; | 148 parsed_payload->type.Video.codec = kRtpVideoGeneric; |
111 parsed_payload->type.Video.width = 0; | 149 parsed_payload->type.Video.width = 0; |
112 parsed_payload->type.Video.height = 0; | 150 parsed_payload->type.Video.height = 0; |
113 | 151 |
114 parsed_payload->payload = payload_data; | 152 parsed_payload->payload = payload_data; |
115 parsed_payload->payload_length = payload_data_length; | 153 parsed_payload->payload_length = payload_data_length; |
116 return true; | 154 return true; |
117 } | 155 } |
118 } // namespace webrtc | 156 } // namespace webrtc |
OLD | NEW |