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 &o) = 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 
38  RTPDataPacket& operator=(const RTPDataPacket &rhs) = default;
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_
uint GetCSRCCount(void) const
Definition: rtpdatapacket.h:79
uint GetTimeStamp(void) const
RTPDataPacket(const UDPPacket &o)
Definition: rtpdatapacket.h:34
uint GetVersion(void) const
Definition: rtpdatapacket.h:76
UDP Packet.
Definition: udppacket.h:20
uint GetSynchronizationSource(void) const
uint GetSequenceNumber(void) const
Definition: rtpdatapacket.h:95
uint GetPayloadType(void) const
Definition: rtpdatapacket.h:90
uint GetContributingSource(uint i) const
static guint32 * tmp
Definition: goom_core.c:35
RTP Data Packet.
Definition: rtpdatapacket.h:30
uint GetPaddingSize(void) const
bool HasPadding(void) const
Definition: rtpdatapacket.h:77
RTPDataPacket(void)
Definition: rtpdatapacket.h:36
unsigned int uint
Definition: compat.h:140
bool HasExtension(void) const
Definition: rtpdatapacket.h:78
RTPDataPacket & operator=(const RTPDataPacket &rhs)=default
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QByteArray m_data
Definition: udppacket.h:42
bool IsValid(void) const override
IsValid() must return true before any data access methods are called, other than GetDataReference() a...
Definition: rtpdatapacket.h:40
uint GetPayloadOffset(void) const
RTPDataPacket(uint64_t key)
Definition: rtpdatapacket.h:35