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 "compat.h" // for random on windows
13 
14 PacketBuffer::PacketBuffer(unsigned int bitrate) :
15  m_bitrate(bitrate),
16  m_next_empty_packet_key(0ULL)
17 {
19  {
21  (random() << 24) ^ (random() << 16) ^
22  (random() << 8) ^ random();
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 }
UDPPacket GetEmptyPacket(void)
Gets a packet for use in PushDataPacket/PushFECPacket.
UDP Packet.
Definition: udppacket.h:20
QMap< uint64_t, UDPPacket > m_empty_packets
Packets ready for reuse.
Definition: packetbuffer.h:50
uint64_t GetKey(void) const
Definition: udppacket.h:39
bool HasAvailablePacket(void) const
Returns true if there are ordered packets ready for processing.
PacketBuffer(unsigned int bitrate)
void FreePacket(const UDPPacket &)
Frees an RTPDataPacket returned by PopDataPacket.
UDPPacket PopDataPacket(void)
Fetches a data packet for processing.
static long int random(void)
Definition: compat.h:149
uint64_t m_next_empty_packet_key
Packets key to use for next empty packet.
Definition: packetbuffer.h:47
QList< UDPPacket > m_available_packets
Ordered list of available packets.
Definition: packetbuffer.h:53