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 "webrtc/modules/rtp_rtcp/source/rtp_format_h264.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtp_format_h264.h" |
12 | 12 |
13 #include <string.h> | 13 #include <string.h> |
14 #include <memory> | 14 #include <memory> |
15 #include <utility> | 15 #include <utility> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
20 #include "webrtc/modules/include/module_common_types.h" | 20 #include "webrtc/modules/include/module_common_types.h" |
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 22 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h" |
22 #include "webrtc/common_video/h264/sps_vui_rewriter.h" | 23 #include "webrtc/common_video/h264/sps_vui_rewriter.h" |
23 #include "webrtc/common_video/h264/h264_common.h" | 24 #include "webrtc/common_video/h264/h264_common.h" |
24 #include "webrtc/common_video/h264/pps_parser.h" | 25 #include "webrtc/common_video/h264/pps_parser.h" |
25 #include "webrtc/common_video/h264/sps_parser.h" | 26 #include "webrtc/common_video/h264/sps_parser.h" |
26 #include "webrtc/system_wrappers/include/metrics.h" | 27 #include "webrtc/system_wrappers/include/metrics.h" |
27 | 28 |
28 namespace webrtc { | 29 namespace webrtc { |
29 namespace { | 30 namespace { |
30 | 31 |
31 static const size_t kNalHeaderSize = 1; | 32 static const size_t kNalHeaderSize = 1; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // we need to add the STAP-A NALU header and a length field for the first | 223 // we need to add the STAP-A NALU header and a length field for the first |
223 // NALU of this packet. | 224 // NALU of this packet. |
224 if (aggregated_fragments == 0) | 225 if (aggregated_fragments == 0) |
225 fragment_headers_length += kNalHeaderSize + kLengthFieldSize; | 226 fragment_headers_length += kNalHeaderSize + kLengthFieldSize; |
226 ++aggregated_fragments; | 227 ++aggregated_fragments; |
227 } | 228 } |
228 packets_.back().last_fragment = true; | 229 packets_.back().last_fragment = true; |
229 return fragment_index; | 230 return fragment_index; |
230 } | 231 } |
231 | 232 |
232 bool RtpPacketizerH264::NextPacket(uint8_t* buffer, | 233 bool RtpPacketizerH264::NextPacket(RtpPacketToSend* rtp_packet, |
233 size_t* bytes_to_send, | |
234 bool* last_packet) { | 234 bool* last_packet) { |
235 *bytes_to_send = 0; | 235 RTC_DCHECK(rtp_packet); |
| 236 RTC_DCHECK(last_packet); |
236 if (packets_.empty()) { | 237 if (packets_.empty()) { |
237 *bytes_to_send = 0; | |
238 *last_packet = true; | 238 *last_packet = true; |
239 return false; | 239 return false; |
240 } | 240 } |
241 | 241 |
242 PacketUnit packet = packets_.front(); | 242 PacketUnit packet = packets_.front(); |
243 | |
244 if (packet.first_fragment && packet.last_fragment) { | 243 if (packet.first_fragment && packet.last_fragment) { |
245 // Single NAL unit packet. | 244 // Single NAL unit packet. |
246 *bytes_to_send = packet.source_fragment.length; | 245 size_t bytes_to_send = packet.source_fragment.length; |
247 memcpy(buffer, packet.source_fragment.buffer, *bytes_to_send); | 246 uint8_t* buffer = rtp_packet->AllocatePayload(bytes_to_send); |
| 247 memcpy(buffer, packet.source_fragment.buffer, bytes_to_send); |
248 packets_.pop(); | 248 packets_.pop(); |
249 input_fragments_.pop_front(); | 249 input_fragments_.pop_front(); |
250 RTC_CHECK_LE(*bytes_to_send, max_payload_len_); | |
251 } else if (packet.aggregated) { | 250 } else if (packet.aggregated) { |
252 NextAggregatePacket(buffer, bytes_to_send); | 251 NextAggregatePacket(rtp_packet); |
253 RTC_CHECK_LE(*bytes_to_send, max_payload_len_); | |
254 } else { | 252 } else { |
255 NextFragmentPacket(buffer, bytes_to_send); | 253 NextFragmentPacket(rtp_packet); |
256 RTC_CHECK_LE(*bytes_to_send, max_payload_len_); | |
257 } | 254 } |
| 255 RTC_DCHECK_LE(rtp_packet->payload_size(), max_payload_len_); |
258 *last_packet = packets_.empty(); | 256 *last_packet = packets_.empty(); |
| 257 rtp_packet->SetMarker(*last_packet); |
259 return true; | 258 return true; |
260 } | 259 } |
261 | 260 |
262 void RtpPacketizerH264::NextAggregatePacket(uint8_t* buffer, | 261 void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet) { |
263 size_t* bytes_to_send) { | 262 uint8_t* buffer = rtp_packet->AllocatePayload(max_payload_len_); |
| 263 RTC_DCHECK(buffer); |
264 PacketUnit* packet = &packets_.front(); | 264 PacketUnit* packet = &packets_.front(); |
265 RTC_CHECK(packet->first_fragment); | 265 RTC_CHECK(packet->first_fragment); |
266 // STAP-A NALU header. | 266 // STAP-A NALU header. |
267 buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA; | 267 buffer[0] = (packet->header & (kFBit | kNriMask)) | H264::NaluType::kStapA; |
268 int index = kNalHeaderSize; | 268 size_t index = kNalHeaderSize; |
269 *bytes_to_send += kNalHeaderSize; | |
270 while (packet->aggregated) { | 269 while (packet->aggregated) { |
271 const Fragment& fragment = packet->source_fragment; | 270 const Fragment& fragment = packet->source_fragment; |
272 // Add NAL unit length field. | 271 // Add NAL unit length field. |
273 ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.length); | 272 ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.length); |
274 index += kLengthFieldSize; | 273 index += kLengthFieldSize; |
275 *bytes_to_send += kLengthFieldSize; | |
276 // Add NAL unit. | 274 // Add NAL unit. |
277 memcpy(&buffer[index], fragment.buffer, fragment.length); | 275 memcpy(&buffer[index], fragment.buffer, fragment.length); |
278 index += fragment.length; | 276 index += fragment.length; |
279 *bytes_to_send += fragment.length; | |
280 packets_.pop(); | 277 packets_.pop(); |
281 input_fragments_.pop_front(); | 278 input_fragments_.pop_front(); |
282 if (packet->last_fragment) | 279 if (packet->last_fragment) |
283 break; | 280 break; |
284 packet = &packets_.front(); | 281 packet = &packets_.front(); |
285 } | 282 } |
286 RTC_CHECK(packet->last_fragment); | 283 RTC_CHECK(packet->last_fragment); |
| 284 rtp_packet->SetPayloadSize(index); |
287 } | 285 } |
288 | 286 |
289 void RtpPacketizerH264::NextFragmentPacket(uint8_t* buffer, | 287 void RtpPacketizerH264::NextFragmentPacket(RtpPacketToSend* rtp_packet) { |
290 size_t* bytes_to_send) { | |
291 PacketUnit* packet = &packets_.front(); | 288 PacketUnit* packet = &packets_.front(); |
292 // NAL unit fragmented over multiple packets (FU-A). | 289 // NAL unit fragmented over multiple packets (FU-A). |
293 // We do not send original NALU header, so it will be replaced by the | 290 // We do not send original NALU header, so it will be replaced by the |
294 // FU indicator header of the first packet. | 291 // FU indicator header of the first packet. |
295 uint8_t fu_indicator = | 292 uint8_t fu_indicator = |
296 (packet->header & (kFBit | kNriMask)) | H264::NaluType::kFuA; | 293 (packet->header & (kFBit | kNriMask)) | H264::NaluType::kFuA; |
297 uint8_t fu_header = 0; | 294 uint8_t fu_header = 0; |
298 | 295 |
299 // S | E | R | 5 bit type. | 296 // S | E | R | 5 bit type. |
300 fu_header |= (packet->first_fragment ? kSBit : 0); | 297 fu_header |= (packet->first_fragment ? kSBit : 0); |
301 fu_header |= (packet->last_fragment ? kEBit : 0); | 298 fu_header |= (packet->last_fragment ? kEBit : 0); |
302 uint8_t type = packet->header & kTypeMask; | 299 uint8_t type = packet->header & kTypeMask; |
303 fu_header |= type; | 300 fu_header |= type; |
| 301 const Fragment& fragment = packet->source_fragment; |
| 302 uint8_t* buffer = |
| 303 rtp_packet->AllocatePayload(kFuAHeaderSize + fragment.length); |
304 buffer[0] = fu_indicator; | 304 buffer[0] = fu_indicator; |
305 buffer[1] = fu_header; | 305 buffer[1] = fu_header; |
306 | |
307 const Fragment& fragment = packet->source_fragment; | |
308 *bytes_to_send = fragment.length + kFuAHeaderSize; | |
309 memcpy(buffer + kFuAHeaderSize, fragment.buffer, fragment.length); | 306 memcpy(buffer + kFuAHeaderSize, fragment.buffer, fragment.length); |
310 if (packet->last_fragment) | 307 if (packet->last_fragment) |
311 input_fragments_.pop_front(); | 308 input_fragments_.pop_front(); |
312 packets_.pop(); | 309 packets_.pop(); |
313 } | 310 } |
314 | 311 |
315 ProtectionType RtpPacketizerH264::GetProtectionType() { | 312 ProtectionType RtpPacketizerH264::GetProtectionType() { |
316 return kProtectedPacket; | 313 return kProtectedPacket; |
317 } | 314 } |
318 | 315 |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 h264->packetization_type = kH264FuA; | 592 h264->packetization_type = kH264FuA; |
596 h264->nalu_type = original_nal_type; | 593 h264->nalu_type = original_nal_type; |
597 if (first_fragment) { | 594 if (first_fragment) { |
598 h264->nalus[h264->nalus_length] = nalu; | 595 h264->nalus[h264->nalus_length] = nalu; |
599 h264->nalus_length = 1; | 596 h264->nalus_length = 1; |
600 } | 597 } |
601 return true; | 598 return true; |
602 } | 599 } |
603 | 600 |
604 } // namespace webrtc | 601 } // namespace webrtc |
OLD | NEW |