1 /* 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 11 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <deque> 18 #include <memory> 19 #include <queue> 20 21 #include "api/array_view.h" 22 #include "modules/rtp_rtcp/source/rtp_format.h" 23 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" 24 #include "modules/video_coding/codecs/h264/include/h264_globals.h" 25 #include "rtc_base/buffer.h" 26 27 namespace webrtc { 28 29 // Bit masks for NAL (F, NRI, Type) indicators. 30 constexpr uint8_t kH264FBit = 0x80; 31 constexpr uint8_t kH264NriMask = 0x60; 32 constexpr uint8_t kH264TypeMask = 0x1F; 33 34 // Bit masks for FU (A and B) headers. 35 constexpr uint8_t kH264SBit = 0x80; 36 constexpr uint8_t kH264EBit = 0x40; 37 constexpr uint8_t kH264RBit = 0x20; 38 39 class RtpPacketizerH264 : public RtpPacketizer { 40 public: 41 // Initialize with payload from encoder. 42 // The payload_data must be exactly one encoded H264 frame. 43 RtpPacketizerH264(rtc::ArrayView<const uint8_t> payload, 44 PayloadSizeLimits limits, 45 H264PacketizationMode packetization_mode); 46 47 ~RtpPacketizerH264() override; 48 49 RtpPacketizerH264(const RtpPacketizerH264&) = delete; 50 RtpPacketizerH264& operator=(const RtpPacketizerH264&) = delete; 51 52 size_t NumPackets() const override; 53 54 // Get the next payload with H264 payload header. 55 // Write payload and set marker bit of the `packet`. 56 // Returns true on success, false otherwise. 57 bool NextPacket(RtpPacketToSend* rtp_packet) override; 58 59 private: 60 // A packet unit (H264 packet), to be put into an RTP packet: 61 // If a NAL unit is too large for an RTP packet, this packet unit will 62 // represent a FU-A packet of a single fragment of the NAL unit. 63 // If a NAL unit is small enough to fit within a single RTP packet, this 64 // packet unit may represent a single NAL unit or a STAP-A packet, of which 65 // there may be multiple in a single RTP packet (if so, aggregated = true). 66 struct PacketUnit { PacketUnitPacketUnit67 PacketUnit(rtc::ArrayView<const uint8_t> source_fragment, 68 bool first_fragment, 69 bool last_fragment, 70 bool aggregated, 71 uint8_t header) 72 : source_fragment(source_fragment), 73 first_fragment(first_fragment), 74 last_fragment(last_fragment), 75 aggregated(aggregated), 76 header(header) {} 77 78 rtc::ArrayView<const uint8_t> source_fragment; 79 bool first_fragment; 80 bool last_fragment; 81 bool aggregated; 82 uint8_t header; 83 }; 84 85 bool GeneratePackets(H264PacketizationMode packetization_mode); 86 bool PacketizeFuA(size_t fragment_index); 87 size_t PacketizeStapA(size_t fragment_index); 88 bool PacketizeSingleNalu(size_t fragment_index); 89 90 void NextAggregatePacket(RtpPacketToSend* rtp_packet); 91 void NextFragmentPacket(RtpPacketToSend* rtp_packet); 92 93 const PayloadSizeLimits limits_; 94 size_t num_packets_left_; 95 std::deque<rtc::ArrayView<const uint8_t>> input_fragments_; 96 std::queue<PacketUnit> packets_; 97 }; 98 } // namespace webrtc 99 #endif // MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_ 100