Line data Source code
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 WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
12 : #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
13 :
14 : #include <deque>
15 : #include <memory>
16 : #include <queue>
17 : #include <string>
18 :
19 : #include "webrtc/base/buffer.h"
20 : #include "webrtc/base/constructormagic.h"
21 : #include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
22 :
23 : namespace webrtc {
24 :
25 : class RtpPacketizerH264 : public RtpPacketizer {
26 : public:
27 : // Initialize with payload from encoder.
28 : // The payload_data must be exactly one encoded H264 frame.
29 : RtpPacketizerH264(size_t max_payload_len,
30 : H264PacketizationMode packetization_mode);
31 :
32 : virtual ~RtpPacketizerH264();
33 :
34 : void SetPayloadData(const uint8_t* payload_data,
35 : size_t payload_size,
36 : const RTPFragmentationHeader* fragmentation) override;
37 :
38 : // Get the next payload with H264 payload header.
39 : // Write payload and set marker bit of the |packet|.
40 : // The parameter |last_packet| is true for the last packet of the frame, false
41 : // otherwise (i.e., call the function again to get the next packet).
42 : // Returns true on success, false otherwise.
43 : bool NextPacket(RtpPacketToSend* rtp_packet, bool* last_packet) override;
44 :
45 : ProtectionType GetProtectionType() override;
46 :
47 : StorageType GetStorageType(uint32_t retransmission_settings) override;
48 :
49 : std::string ToString() override;
50 :
51 : private:
52 : // Input fragments (NAL units), with an optionally owned temporary buffer,
53 : // used in case the fragment gets modified.
54 0 : struct Fragment {
55 : Fragment(const uint8_t* buffer, size_t length);
56 : explicit Fragment(const Fragment& fragment);
57 : const uint8_t* buffer = nullptr;
58 : size_t length = 0;
59 : std::unique_ptr<rtc::Buffer> tmp_buffer;
60 : };
61 :
62 : // A packet unit (H264 packet), to be put into an RTP packet:
63 : // If a NAL unit is too large for an RTP packet, this packet unit will
64 : // represent a FU-A packet of a single fragment of the NAL unit.
65 : // If a NAL unit is small enough to fit within a single RTP packet, this
66 : // packet unit may represent a single NAL unit or a STAP-A packet, of which
67 : // there may be multiple in a single RTP packet (if so, aggregated = true).
68 0 : struct PacketUnit {
69 0 : PacketUnit(const Fragment& source_fragment,
70 : bool first_fragment,
71 : bool last_fragment,
72 : bool aggregated,
73 : uint8_t header)
74 0 : : source_fragment(source_fragment),
75 : first_fragment(first_fragment),
76 : last_fragment(last_fragment),
77 : aggregated(aggregated),
78 0 : header(header) {}
79 :
80 : const Fragment source_fragment;
81 : bool first_fragment;
82 : bool last_fragment;
83 : bool aggregated;
84 : uint8_t header;
85 : };
86 :
87 : void GeneratePackets();
88 : void PacketizeFuA(size_t fragment_index);
89 : size_t PacketizeStapA(size_t fragment_index);
90 : void PacketizeSingleNalu(size_t fragment_index);
91 : void NextAggregatePacket(RtpPacketToSend* rtp_packet);
92 : void NextFragmentPacket(RtpPacketToSend* rtp_packet);
93 :
94 : const size_t max_payload_len_;
95 : const H264PacketizationMode packetization_mode_;
96 : std::deque<Fragment> input_fragments_;
97 : std::queue<PacketUnit> packets_;
98 :
99 : RTC_DISALLOW_COPY_AND_ASSIGN(RtpPacketizerH264);
100 : };
101 :
102 : // Depacketizer for H264.
103 : class RtpDepacketizerH264 : public RtpDepacketizer {
104 : public:
105 : RtpDepacketizerH264();
106 : virtual ~RtpDepacketizerH264();
107 :
108 : bool Parse(ParsedPayload* parsed_payload,
109 : const uint8_t* payload_data,
110 : size_t payload_data_length) override;
111 :
112 : private:
113 : bool ParseFuaNalu(RtpDepacketizer::ParsedPayload* parsed_payload,
114 : const uint8_t* payload_data);
115 : bool ProcessStapAOrSingleNalu(RtpDepacketizer::ParsedPayload* parsed_payload,
116 : const uint8_t* payload_data);
117 :
118 : size_t offset_;
119 : size_t length_;
120 : std::unique_ptr<rtc::Buffer> modified_buffer_;
121 : };
122 : } // namespace webrtc
123 : #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_H264_H_
|