MythTV  master
packetbuffer.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * PacketBuffer
3  * Copyright (c) 2012 Digital Nirvana, Inc.
4  * Distributed as part of MythTV under GPL v2 and later.
5  */
6 
7 // POSIX headers
8 #include <cstdlib> // for random
9 
10 // MythTV headers
11 #include "packetbuffer.h"
12 #include "mythmiscutil.h"
13 
14 PacketBuffer::PacketBuffer(unsigned int bitrate) :
15  m_bitrate(bitrate),
16  m_next_empty_packet_key(0ULL)
17 {
19  {
21  (MythRandom() << 24) ^ (MythRandom() << 16) ^
22  (MythRandom() << 8) ^ MythRandom();
23  }
24 }
25 
27 {
28  return !m_available_packets.empty();
29 }
30 
32 {
33  if (m_available_packets.empty())
34  return UDPPacket(0);
35 
36  UDPPacket packet(m_available_packets.front());
37  m_available_packets.pop_front();
38 
39  return packet;
40 }
41 
43 {
44  QMap<uint64_t, UDPPacket>::iterator it = m_empty_packets.begin();
45  if (it == m_empty_packets.end())
46  {
48  }
49 
50  UDPPacket packet(*it);
51  m_empty_packets.erase(it);
52 
53  return packet;
54 }
55 
57 {
58  uint64_t top = packet.GetKey() & (0xFFFFFFFFULL<<32);
59  if (top == (m_next_empty_packet_key & (0xFFFFFFFFULL<<32)))
60  m_empty_packets[packet.GetKey()] = packet;
61 }
PacketBuffer::FreePacket
void FreePacket(const UDPPacket &packet)
Frees an RTPDataPacket returned by PopDataPacket.
Definition: packetbuffer.cpp:56
PacketBuffer::m_next_empty_packet_key
uint64_t m_next_empty_packet_key
Packets key to use for next empty packet.
Definition: packetbuffer.h:47
PacketBuffer::m_available_packets
QList< UDPPacket > m_available_packets
Ordered list of available packets.
Definition: packetbuffer.h:53
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:24
mythmiscutil.h
PacketBuffer::HasAvailablePacket
bool HasAvailablePacket(void) const
Returns true if there are ordered packets ready for processing.
Definition: packetbuffer.cpp:26
PacketBuffer::m_empty_packets
QMap< uint64_t, UDPPacket > m_empty_packets
Packets ready for reuse.
Definition: packetbuffer.h:50
PacketBuffer::PacketBuffer
PacketBuffer(unsigned int bitrate)
Definition: packetbuffer.cpp:14
PacketBuffer::PopDataPacket
UDPPacket PopDataPacket(void)
Fetches a data packet for processing.
Definition: packetbuffer.cpp:31
UDPPacket
UDP Packet.
Definition: udppacket.h:21
packetbuffer.h
UDPPacket::GetKey
uint64_t GetKey(void) const
Definition: udppacket.h:34
PacketBuffer::GetEmptyPacket
UDPPacket GetEmptyPacket(void)
Gets a packet for use in PushDataPacket/PushFECPacket.
Definition: packetbuffer.cpp:42