MythTV  master
rtpdatapacket.h
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Copyright (c) 2012 Digital Nirvana, Inc.
3  * Distributed as part of MythTV under GPL v2 and later.
4  */
5 
6 #ifndef RTP_DATA_PACKET_H
7 #define RTP_DATA_PACKET_H
8 
9 #include <arpa/inet.h> // for ntohs()/ntohl()
10 
11 #include "udppacket.h"
12 #include "mythlogging.h"
13 
14 #ifdef _MSC_VER
15 # include <WinSock2.h>
16 #endif
17 
30 class RTPDataPacket : public UDPPacket
31 {
32  public:
33  RTPDataPacket(const RTPDataPacket&) = default;
34  explicit RTPDataPacket(const UDPPacket &o) : UDPPacket(o), m_off(0) { }
35  explicit RTPDataPacket(uint64_t key) : UDPPacket(key), m_off(0) { }
36  RTPDataPacket(void) : UDPPacket(0ULL) { }
37 
39 
40  bool IsValid(void) const override // UDPPacket
41  {
42  if (m_data.size() < 12)
43  {
44  return false;
45  }
46  if (2 != GetVersion())
47  {
48  LOG(VB_GENERAL, LOG_INFO, QString("Version incorrect %1")
49  .arg(GetVersion()));
50  return false;
51  }
52 
53  int off = 12 + 4 * GetCSRCCount();
54  if (off > m_data.size())
55  {
56  LOG(VB_GENERAL, LOG_INFO, QString("off %1 > sz %2")
57  .arg(off).arg(m_data.size()));
58  return false;
59  }
60  if (HasExtension())
61  {
62  uint ext_size = m_data[off+2] << 8 | m_data[off+3];
63  off += 4 * (1 + ext_size);
64  }
65  if (off > m_data.size())
66  {
67  LOG(VB_GENERAL, LOG_INFO, QString("off + ext %1 > sz %2")
68  .arg(off).arg(m_data.size()));
69  return false;
70  }
71  m_off = off;
72 
73  return true;
74  }
75 
76  uint GetVersion(void) const { return (m_data[0] >> 6) & 0x3; }
77  bool HasPadding(void) const { return (m_data[0] >> 5) & 0x1; }
78  bool HasExtension(void) const { return (m_data[0] >> 4) & 0x1; }
79  uint GetCSRCCount(void) const { return m_data[0] & 0xf; }
80 
81  enum {
88  };
89 
90  uint GetPayloadType(void) const
91  {
92  return m_data[1] & 0x7f;
93  }
94 
95  uint GetSequenceNumber(void) const
96  {
97  return ntohs(*reinterpret_cast<const uint16_t*>(m_data.data()+2));
98  }
99 
100  uint GetTimeStamp(void) const
101  {
102  return ntohl(*reinterpret_cast<const uint32_t*>(m_data.data()+4));
103  }
104 
106  {
107  return ntohl(*reinterpret_cast<const uint32_t*>(m_data.data()+8));
108  }
109 
111  {
112  const uint32_t tmp =
113  *reinterpret_cast<const uint32_t*>(m_data.data() + 12 + 4 * i);
114  return ntohl(tmp);
115  }
116 
117  uint GetPayloadOffset(void) const { return m_off; }
118 
119  uint GetPaddingSize(void) const
120  {
121  if (!HasPadding())
122  return 0;
123  return m_data[m_data.size()-1];
124  }
125 
126  protected:
127  mutable uint m_off { 0 };
128 };
129 
130 #endif // RTP_DATA_PACKET_H
RTPDataPacket::GetTimeStamp
uint GetTimeStamp(void) const
Definition: rtpdatapacket.h:100
RTPDataPacket::kPayLoadTypeH261Video
@ kPayLoadTypeH261Video
Definition: rtpdatapacket.h:84
RTPDataPacket::kPayLoadTypeTS
@ kPayLoadTypeTS
Definition: rtpdatapacket.h:86
RTPDataPacket::operator=
RTPDataPacket & operator=(const RTPDataPacket &)=default
arg
arg(title).arg(filename).arg(doDelete))
RTPDataPacket::GetVersion
uint GetVersion(void) const
Definition: rtpdatapacket.h:76
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
tmp
static guint32 * tmp
Definition: goom_core.cpp:30
UDPPacket::m_data
QByteArray m_data
Definition: udppacket.h:42
RTPDataPacket::GetContributingSource
uint GetContributingSource(uint i) const
Definition: rtpdatapacket.h:110
RTPDataPacket::RTPDataPacket
RTPDataPacket(void)
Definition: rtpdatapacket.h:36
RTPDataPacket::GetPayloadOffset
uint GetPayloadOffset(void) const
Definition: rtpdatapacket.h:117
RTPDataPacket::RTPDataPacket
RTPDataPacket(const UDPPacket &o)
Definition: rtpdatapacket.h:34
mythlogging.h
RTPDataPacket::GetSynchronizationSource
uint GetSynchronizationSource(void) const
Definition: rtpdatapacket.h:105
RTPDataPacket::kPayLoadTypeH263Video
@ kPayLoadTypeH263Video
Definition: rtpdatapacket.h:87
RTPDataPacket::GetCSRCCount
uint GetCSRCCount(void) const
Definition: rtpdatapacket.h:79
RTPDataPacket::HasPadding
bool HasPadding(void) const
Definition: rtpdatapacket.h:77
uint
unsigned int uint
Definition: compat.h:140
RTPDataPacket::GetPaddingSize
uint GetPaddingSize(void) const
Definition: rtpdatapacket.h:119
RTPDataPacket::GetSequenceNumber
uint GetSequenceNumber(void) const
Definition: rtpdatapacket.h:95
udppacket.h
RTPDataPacket::kPayLoadTypeMPEG2Video
@ kPayLoadTypeMPEG2Video
Definition: rtpdatapacket.h:85
RTPDataPacket::kPayLoadTypeMPEGAudio
@ kPayLoadTypeMPEGAudio
Definition: rtpdatapacket.h:83
RTPDataPacket::GetPayloadType
uint GetPayloadType(void) const
Definition: rtpdatapacket.h:90
RTPDataPacket::m_off
uint m_off
Definition: rtpdatapacket.h:127
RTPDataPacket::RTPDataPacket
RTPDataPacket(const RTPDataPacket &)=default
RTPDataPacket::HasExtension
bool HasExtension(void) const
Definition: rtpdatapacket.h:78
RTPDataPacket
RTP Data Packet.
Definition: rtpdatapacket.h:31
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:1
RTPDataPacket::RTPDataPacket
RTPDataPacket(uint64_t key)
Definition: rtpdatapacket.h:35
UDPPacket
UDP Packet.
Definition: udppacket.h:21
RTPDataPacket::IsValid
bool IsValid(void) const override
IsValid() must return true before any data access methods are called, other than GetDataReference() a...
Definition: rtpdatapacket.h:40
RTPDataPacket::kPayLoadTypePCMAudio
@ kPayLoadTypePCMAudio
Definition: rtpdatapacket.h:82