VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvrtppacket.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program 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
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // grtppacket.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gvrtppacket.h"
23 #include "gstr.h"
24 #include "gassert.h"
25 #include "ghexdump.h"
26 #include "glog.h"
27 #include <sstream>
28 #include <algorithm>
29 
30 Gv::RtpPacket::RtpPacket( const char * p , size_t n ) :
31  m_p(reinterpret_cast<const unsigned char*>(p)) ,
32  m_n(n)
33 {
34  G_ASSERT( n >= smallest() ) ;
35 
36  m_version = ( m_p[0] & 0xc0 ) >> 6 ;
37  m_padding = !!( m_p[0] & 0x20 ) ;
38  m_extension = !!( m_p[0] & 0x10 ) ;
39  m_cc = m_p[0] & 0x0f ;
40  m_marker = !!( m_p[1] & 0x80 ) ;
41  m_pt = m_p[1] & 0x7f ;
42  m_seq = make_word( &m_p[2] ) ;
43  m_timestamp = make_dword( &m_p[4] ) ;
44  m_src_id = make_dword( &m_p[8] ) ;
45 
46  m_header_size = 12U + 4U*m_cc ;
47  m_ehl = (m_extension && n>=(m_header_size+4U)) ? make_word(&m_p[m_header_size+2U]) : 0U ;
48  m_header_size += (m_ehl*4U) ;
49 
50  m_padsize = m_padding ? m_p[n-1U] : 0U ;
51  m_payload_size = n - m_header_size - m_padsize ; // underflow checked later
52 
53  if( m_version != 2U ) m_reason = "invalid version" ;
54  if( n < (m_header_size+m_padsize) ) m_reason = "invalid sizes" ;
55 }
56 
58 {
59  return 12U ;
60 }
61 
63 {
64  return m_reason.empty() ;
65 }
66 
67 std::string Gv::RtpPacket::reason() const
68 {
69  return m_reason ;
70 }
71 
72 std::string Gv::RtpPacket::str() const
73 {
74  std::ostringstream ss ;
75  ss
76  << "n=" << m_n
77  << " v=" << m_version << " p=" << m_padding << " e=" << m_extension
78  << " cc=" << m_cc << " m=" << m_marker << " pt=" << m_pt
79  << " ehl=" << m_ehl << " hs=" << m_header_size
80  << " pds=" << m_padsize << " pls=" << m_payload_size
81  << " ts=" << m_timestamp
82  << " seq=" << m_seq ;
83  return ss.str() ;
84 }
85 
86 std::string Gv::RtpPacket::hexdump() const
87 {
88  std::ostringstream ss ;
89  ss << G::hexdump<8>(m_p,m_p+m_n) ;
90  return ss.str() ;
91 }
92 
94 {
95  return m_marker ;
96 }
97 
98 unsigned int Gv::RtpPacket::seq() const
99 {
100  return m_seq ;
101 }
102 
103 unsigned long Gv::RtpPacket::timestamp() const
104 {
105  return m_timestamp ;
106 }
107 
108 unsigned long Gv::RtpPacket::src() const
109 {
110  return m_src_id ;
111 }
112 
113 unsigned int Gv::RtpPacket::make_word( const unsigned char * p )
114 {
115  return (static_cast<unsigned int>(p[0]) << 8) | p[1] ;
116 }
117 
118 unsigned long Gv::RtpPacket::make_dword( const unsigned char * p )
119 {
120  return
121  (static_cast<unsigned long>(p[0]) << 24) |
122  (static_cast<unsigned long>(p[1]) << 16) |
123  (static_cast<unsigned long>(p[2]) << 8) |
124  (static_cast<unsigned long>(p[3]) << 0) ;
125 }
126 
127 unsigned int Gv::RtpPacket::type() const
128 {
129  return m_pt ;
130 }
131 
133 {
134  // See RFC 3551 table 5
135  return m_pt == 26U ;
136 }
137 
139 {
140  // See RFC 3551 table 5
141  return m_pt >= 96U && m_pt <= 127U ;
142 }
143 
144 const unsigned char * Gv::RtpPacket::ubegin() const
145 {
146  return m_p + m_header_size ;
147 }
148 
149 const unsigned char * Gv::RtpPacket::uend() const
150 {
151  return m_p + m_header_size + m_payload_size ;
152 }
153 
154 const char * Gv::RtpPacket::begin() const
155 {
156  return reinterpret_cast<const char*>( ubegin() ) ;
157 }
158 
159 const char * Gv::RtpPacket::end() const
160 {
161  return reinterpret_cast<const char*>( uend() ) ;
162 }
163 
164 size_t Gv::RtpPacket::offset() const
165 {
166  return m_header_size ;
167 }
168 
169 size_t Gv::RtpPacket::size() const
170 {
171  return m_payload_size ;
172 }
173 
174 /// \file gvrtppacket.cpp
const char * end() const
Returns the payload end iterator.
bool valid() const
Returns true if a valid packet.
Definition: gvrtppacket.cpp:62
bool typeJpeg() const
Returns true if the payload type is 26.
static size_t smallest()
Returns the smallest valid packet size.
Definition: gvrtppacket.cpp:57
std::string str() const
Returns a one-line summary of header fields.
Definition: gvrtppacket.cpp:72
size_t size() const
Returns the payload size.
Synopsis:
std::string hexdump() const
Returns a complete hex dump.
Definition: gvrtppacket.cpp:86
const char * begin() const
Returns the payload begin iterator.
const unsigned char * ubegin() const
Returns the payload begin iterator.
bool typeDynamic() const
Returns true if the payload type is in the dynamic range, 96 to 127.
const unsigned char * uend() const
Returns the payload end iterator.
unsigned long timestamp() const
Returns the timestamp.
RtpPacket(const char *p, size_t n)
Constructor.
Definition: gvrtppacket.cpp:30
size_t offset() const
Returns the payload offset.
unsigned int seq() const
Returns the sequence number.
Definition: gvrtppacket.cpp:98
bool marker() const
Returns the marker bit.
Definition: gvrtppacket.cpp:93
unsigned int type() const
Returns the payload type.
unsigned long src() const
Returns the source id.
std::string reason() const
Returns the in-valid() reason.
Definition: gvrtppacket.cpp:67