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