OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/net/rtp/rtp_packetizer.h" | 5 #include "media/cast/net/rtp/rtp_packetizer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/big_endian.h" | 9 #include "base/big_endian.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 sequence_number_(config_.sequence_number), | 31 sequence_number_(config_.sequence_number), |
32 rtp_timestamp_(0), | 32 rtp_timestamp_(0), |
33 packet_id_(0), | 33 packet_id_(0), |
34 send_packet_count_(0), | 34 send_packet_count_(0), |
35 send_octet_count_(0) { | 35 send_octet_count_(0) { |
36 DCHECK(transport) << "Invalid argument"; | 36 DCHECK(transport) << "Invalid argument"; |
37 } | 37 } |
38 | 38 |
39 RtpPacketizer::~RtpPacketizer() {} | 39 RtpPacketizer::~RtpPacketizer() {} |
40 | 40 |
41 uint16 RtpPacketizer::NextSequenceNumber() { | 41 uint16_t RtpPacketizer::NextSequenceNumber() { |
42 ++sequence_number_; | 42 ++sequence_number_; |
43 return sequence_number_ - 1; | 43 return sequence_number_ - 1; |
44 } | 44 } |
45 | 45 |
46 void RtpPacketizer::SendFrameAsPackets(const EncodedFrame& frame) { | 46 void RtpPacketizer::SendFrameAsPackets(const EncodedFrame& frame) { |
47 uint16 rtp_header_length = kRtpHeaderLength + kCastHeaderLength; | 47 uint16_t rtp_header_length = kRtpHeaderLength + kCastHeaderLength; |
48 uint16 max_length = config_.max_payload_length - rtp_header_length - 1; | 48 uint16_t max_length = config_.max_payload_length - rtp_header_length - 1; |
49 rtp_timestamp_ = frame.rtp_timestamp; | 49 rtp_timestamp_ = frame.rtp_timestamp; |
50 | 50 |
51 // Split the payload evenly (round number up). | 51 // Split the payload evenly (round number up). |
52 size_t num_packets = (frame.data.size() + max_length) / max_length; | 52 size_t num_packets = (frame.data.size() + max_length) / max_length; |
53 size_t payload_length = (frame.data.size() + num_packets) / num_packets; | 53 size_t payload_length = (frame.data.size() + num_packets) / num_packets; |
54 DCHECK_LE(payload_length, max_length) << "Invalid argument"; | 54 DCHECK_LE(payload_length, max_length) << "Invalid argument"; |
55 | 55 |
56 SendPacketVector packets; | 56 SendPacketVector packets; |
57 | 57 |
58 size_t remaining_size = frame.data.size(); | 58 size_t remaining_size = frame.data.size(); |
59 std::string::const_iterator data_iter = frame.data.begin(); | 59 std::string::const_iterator data_iter = frame.data.begin(); |
60 | 60 |
61 uint8 num_extensions = 0; | 61 uint8_t num_extensions = 0; |
62 if (frame.new_playout_delay_ms) | 62 if (frame.new_playout_delay_ms) |
63 num_extensions++; | 63 num_extensions++; |
64 DCHECK_LE(num_extensions, kCastExtensionCountmask); | 64 DCHECK_LE(num_extensions, kCastExtensionCountmask); |
65 | 65 |
66 while (remaining_size > 0) { | 66 while (remaining_size > 0) { |
67 PacketRef packet(new base::RefCountedData<Packet>); | 67 PacketRef packet(new base::RefCountedData<Packet>); |
68 | 68 |
69 if (remaining_size < payload_length) { | 69 if (remaining_size < payload_length) { |
70 payload_length = remaining_size; | 70 payload_length = remaining_size; |
71 } | 71 } |
72 remaining_size -= payload_length; | 72 remaining_size -= payload_length; |
73 BuildCommonRTPheader( | 73 BuildCommonRTPheader( |
74 &packet->data, remaining_size == 0, frame.rtp_timestamp); | 74 &packet->data, remaining_size == 0, frame.rtp_timestamp); |
75 | 75 |
76 // Build Cast header. | 76 // Build Cast header. |
77 // TODO(miu): Should we always set the ref frame bit and the ref_frame_id? | 77 // TODO(miu): Should we always set the ref frame bit and the ref_frame_id? |
78 DCHECK_NE(frame.dependency, EncodedFrame::UNKNOWN_DEPENDENCY); | 78 DCHECK_NE(frame.dependency, EncodedFrame::UNKNOWN_DEPENDENCY); |
79 uint8 byte0 = kCastReferenceFrameIdBitMask; | 79 uint8_t byte0 = kCastReferenceFrameIdBitMask; |
80 if (frame.dependency == EncodedFrame::KEY) | 80 if (frame.dependency == EncodedFrame::KEY) |
81 byte0 |= kCastKeyFrameBitMask; | 81 byte0 |= kCastKeyFrameBitMask; |
82 // Extensions only go on the first packet of the frame | 82 // Extensions only go on the first packet of the frame |
83 if (packet_id_ == 0) | 83 if (packet_id_ == 0) |
84 byte0 |= num_extensions; | 84 byte0 |= num_extensions; |
85 packet->data.push_back(byte0); | 85 packet->data.push_back(byte0); |
86 packet->data.push_back(static_cast<uint8>(frame.frame_id)); | 86 packet->data.push_back(static_cast<uint8_t>(frame.frame_id)); |
87 size_t start_size = packet->data.size(); | 87 size_t start_size = packet->data.size(); |
88 packet->data.resize(start_size + 4); | 88 packet->data.resize(start_size + 4); |
89 base::BigEndianWriter big_endian_writer( | 89 base::BigEndianWriter big_endian_writer( |
90 reinterpret_cast<char*>(&(packet->data[start_size])), 4); | 90 reinterpret_cast<char*>(&(packet->data[start_size])), 4); |
91 big_endian_writer.WriteU16(packet_id_); | 91 big_endian_writer.WriteU16(packet_id_); |
92 big_endian_writer.WriteU16(static_cast<uint16>(num_packets - 1)); | 92 big_endian_writer.WriteU16(static_cast<uint16_t>(num_packets - 1)); |
93 packet->data.push_back(static_cast<uint8>(frame.referenced_frame_id)); | 93 packet->data.push_back(static_cast<uint8_t>(frame.referenced_frame_id)); |
94 // Add extension details only on the first packet of the frame | 94 // Add extension details only on the first packet of the frame |
95 if (packet_id_ == 0 && frame.new_playout_delay_ms) { | 95 if (packet_id_ == 0 && frame.new_playout_delay_ms) { |
96 packet->data.push_back(kCastRtpExtensionAdaptiveLatency << 2); | 96 packet->data.push_back(kCastRtpExtensionAdaptiveLatency << 2); |
97 packet->data.push_back(2); // 2 bytes | 97 packet->data.push_back(2); // 2 bytes |
98 packet->data.push_back( | 98 packet->data.push_back( |
99 static_cast<uint8>(frame.new_playout_delay_ms >> 8)); | 99 static_cast<uint8_t>(frame.new_playout_delay_ms >> 8)); |
100 packet->data.push_back( | 100 packet->data.push_back(static_cast<uint8_t>(frame.new_playout_delay_ms)); |
101 static_cast<uint8>(frame.new_playout_delay_ms)); | |
102 } | 101 } |
103 | 102 |
104 // Copy payload data. | 103 // Copy payload data. |
105 packet->data.insert(packet->data.end(), | 104 packet->data.insert(packet->data.end(), |
106 data_iter, | 105 data_iter, |
107 data_iter + payload_length); | 106 data_iter + payload_length); |
108 data_iter += payload_length; | 107 data_iter += payload_length; |
109 | 108 |
110 const PacketKey key = PacedPacketSender::MakePacketKey( | 109 const PacketKey key = PacedPacketSender::MakePacketKey( |
111 PacketKey::RTP, frame.frame_id, config_.ssrc, packet_id_++); | 110 PacketKey::RTP, frame.frame_id, config_.ssrc, packet_id_++); |
112 packets.push_back(make_pair(key, packet)); | 111 packets.push_back(make_pair(key, packet)); |
113 | 112 |
114 // Update stats. | 113 // Update stats. |
115 ++send_packet_count_; | 114 ++send_packet_count_; |
116 send_octet_count_ += payload_length; | 115 send_octet_count_ += payload_length; |
117 } | 116 } |
118 DCHECK(packet_id_ == num_packets) << "Invalid state"; | 117 DCHECK(packet_id_ == num_packets) << "Invalid state"; |
119 | 118 |
120 packet_storage_->StoreFrame(frame.frame_id, packets); | 119 packet_storage_->StoreFrame(frame.frame_id, packets); |
121 | 120 |
122 // Send to network. | 121 // Send to network. |
123 transport_->SendPackets(packets); | 122 transport_->SendPackets(packets); |
124 | 123 |
125 // Prepare for next frame. | 124 // Prepare for next frame. |
126 packet_id_ = 0; | 125 packet_id_ = 0; |
127 } | 126 } |
128 | 127 |
129 void RtpPacketizer::BuildCommonRTPheader(Packet* packet, | 128 void RtpPacketizer::BuildCommonRTPheader(Packet* packet, |
130 bool marker_bit, | 129 bool marker_bit, |
131 uint32 time_stamp) { | 130 uint32_t time_stamp) { |
132 packet->push_back(0x80); | 131 packet->push_back(0x80); |
133 packet->push_back(static_cast<uint8>(config_.payload_type) | | 132 packet->push_back(static_cast<uint8_t>(config_.payload_type) | |
134 (marker_bit ? kRtpMarkerBitMask : 0)); | 133 (marker_bit ? kRtpMarkerBitMask : 0)); |
135 size_t start_size = packet->size(); | 134 size_t start_size = packet->size(); |
136 packet->resize(start_size + 10); | 135 packet->resize(start_size + 10); |
137 base::BigEndianWriter big_endian_writer( | 136 base::BigEndianWriter big_endian_writer( |
138 reinterpret_cast<char*>(&((*packet)[start_size])), 10); | 137 reinterpret_cast<char*>(&((*packet)[start_size])), 10); |
139 big_endian_writer.WriteU16(sequence_number_); | 138 big_endian_writer.WriteU16(sequence_number_); |
140 big_endian_writer.WriteU32(time_stamp); | 139 big_endian_writer.WriteU32(time_stamp); |
141 big_endian_writer.WriteU32(config_.ssrc); | 140 big_endian_writer.WriteU32(config_.ssrc); |
142 ++sequence_number_; | 141 ++sequence_number_; |
143 } | 142 } |
144 | 143 |
145 } // namespace cast | 144 } // namespace cast |
146 } // namespace media | 145 } // namespace media |
OLD | NEW |