1 /**
2  * Copyright (c) 2020 Filip Klembara (in2core)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef RTC_H264_RTP_PACKETIZER_H
20 #define RTC_H264_RTP_PACKETIZER_H
21 
22 #if RTC_ENABLE_MEDIA
23 
24 #include "mediahandlerrootelement.hpp"
25 #include "nalunit.hpp"
26 #include "rtppacketizer.hpp"
27 
28 namespace rtc {
29 
30 /// RTP packetization of h264 payload
31 class RTC_CPP_EXPORT H264RtpPacketizer final : public RtpPacketizer,
32                                                public MediaHandlerRootElement {
33 	shared_ptr<NalUnits> splitMessage(binary_ptr message);
34 	const uint16_t maximumFragmentSize;
35 
36 public:
37 	/// Default clock rate for H264 in RTP
38 	inline static const uint32_t defaultClockRate = 90 * 1000;
39 
40 	/// NAL unit separator
41 	enum class Separator {
42 		Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
43 		LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE,   // 0x00, 0x00, 0x00, 0x01
44 		ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
45 		StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
46 	};
47 
48 	H264RtpPacketizer(H264RtpPacketizer::Separator separator,
49 	                  shared_ptr<RtpPacketizationConfig> rtpConfig,
50 	                  uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);
51 
52 	/// Constructs h264 payload packetizer with given RTP configuration.
53 	/// @note RTP configuration is used in packetization process which may change some configuration
54 	/// properties such as sequence number.
55 	/// @param rtpConfig  RTP configuration
56 	/// @param maximumFragmentSize maximum size of one NALU fragment
57 	H264RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig,
58 	                  uint16_t maximumFragmentSize = NalUnits::defaultMaximumFragmentSize);
59 
60 	ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
61 	                                                    message_ptr control) override;
62 
63 private:
64 	const Separator separator;
65 };
66 
67 } // namespace rtc
68 
69 #endif /* RTC_ENABLE_MEDIA */
70 
71 #endif /* RTC_H264_RTP_PACKETIZER_H */
72