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_larger_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, calculate average size rounded down. | |
56 payload_len_per_packet_ = total_bytes / num_packets_left_; | |
57 // If we can't divide everything perfectly evenly, we put 1 extra byte in some | |
58 // first packets: 14 bytes in 4 packets would be splitted as 3+3+4+4. | |
59 // There are exactly total_data % num_packets larger packets. | |
60 num_larger_packets_ = total_bytes % num_packets_left_; | |
61 RTC_DCHECK_LE(payload_len_per_packet_, max_payload_len_); | |
45 | 62 |
46 generic_header_ = RtpFormatVideoGeneric::kFirstPacketBit; | 63 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) { | 64 if (frame_type_ == kVideoFrameKey) { |
64 generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit; | 65 generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit; |
65 } | 66 } |
67 return num_packets_left_; | |
68 } | |
69 | |
70 bool RtpPacketizerGeneric::NextPacket(RtpPacketToSend* packet) { | |
71 RTC_DCHECK(packet); | |
72 if (num_packets_left_ == 0) | |
73 return false; | |
74 // Last smaller_packets_ packets are 1 byte smaller than previous packets. | |
danilchap
2017/05/18 11:14:24
update the comment
ilnik
2017/05/18 12:22:52
Done.
| |
75 // Reduce per packet payload once needed. | |
76 if (num_packets_left_ == num_larger_packets_) | |
77 payload_len_per_packet_++; | |
78 size_t next_packet_payload_len = payload_len_per_packet_; | |
79 if (payload_size_ <= next_packet_payload_len) { | |
80 // Whole payload fits into this packet. | |
81 next_packet_payload_len = payload_size_; | |
82 if (num_packets_left_ == 2) { | |
83 // This is the penultimate packet. Leave at least 1 payload byte for the | |
84 // last packet. | |
85 next_packet_payload_len--; | |
86 RTC_DCHECK_GT(next_packet_payload_len, 0); | |
87 } | |
88 } | |
89 RTC_DCHECK_LE(next_packet_payload_len, max_payload_len_); | |
90 | |
91 uint8_t* out_ptr = | |
92 packet->AllocatePayload(kGenericHeaderLength + next_packet_payload_len); | |
93 // Put generic header in packet. | |
66 out_ptr[0] = generic_header_; | 94 out_ptr[0] = generic_header_; |
67 // Remove first-packet bit, following packets are intermediate | 95 // Remove first-packet bit, following packets are intermediate. |
68 generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit; | 96 generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit; |
69 | 97 |
70 // Put payload in packet | 98 // Put payload in packet. |
71 memcpy(out_ptr + kGenericHeaderLength, payload_data_, payload_length_); | 99 memcpy(out_ptr + kGenericHeaderLength, payload_data_, |
72 payload_data_ += payload_length_; | 100 next_packet_payload_len); |
101 payload_data_ += next_packet_payload_len; | |
102 payload_size_ -= next_packet_payload_len; | |
103 num_packets_left_--; | |
104 // Packets left to produce and data left to split should end at the same time. | |
105 RTC_DCHECK_EQ(num_packets_left_ == 0, payload_size_ == 0); | |
73 | 106 |
74 *last_packet = payload_size_ <= 0; | 107 packet->SetMarker(payload_size_ == 0); |
75 packet->SetMarker(*last_packet); | 108 |
76 return true; | 109 return true; |
77 } | 110 } |
78 | 111 |
79 ProtectionType RtpPacketizerGeneric::GetProtectionType() { | 112 ProtectionType RtpPacketizerGeneric::GetProtectionType() { |
80 return kProtectedPacket; | 113 return kProtectedPacket; |
81 } | 114 } |
82 | 115 |
83 StorageType RtpPacketizerGeneric::GetStorageType( | 116 StorageType RtpPacketizerGeneric::GetStorageType( |
84 uint32_t retransmission_settings) { | 117 uint32_t retransmission_settings) { |
85 return kAllowRetransmission; | 118 return kAllowRetransmission; |
(...skipping 23 matching lines...) Expand all Loading... | |
109 (generic_header & RtpFormatVideoGeneric::kFirstPacketBit) != 0; | 142 (generic_header & RtpFormatVideoGeneric::kFirstPacketBit) != 0; |
110 parsed_payload->type.Video.codec = kRtpVideoGeneric; | 143 parsed_payload->type.Video.codec = kRtpVideoGeneric; |
111 parsed_payload->type.Video.width = 0; | 144 parsed_payload->type.Video.width = 0; |
112 parsed_payload->type.Video.height = 0; | 145 parsed_payload->type.Video.height = 0; |
113 | 146 |
114 parsed_payload->payload = payload_data; | 147 parsed_payload->payload = payload_data; |
115 parsed_payload->payload_length = payload_data_length; | 148 parsed_payload->payload_length = payload_data_length; |
116 return true; | 149 return true; |
117 } | 150 } |
118 } // namespace webrtc | 151 } // namespace webrtc |
OLD | NEW |