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 #include <cstdint>
8 
9 // MythTV headers
10 #include "libmythbase/mythrandom.h"
11 #include "packetbuffer.h"
12 
13 PacketBuffer::PacketBuffer(unsigned int bitrate) :
14  m_bitrate(bitrate),
15  m_next_empty_packet_key(static_cast<uint64_t>(MythRandom()) << 32)
16 {
17 }
18 
20 {
21  return !m_available_packets.empty();
22 }
23 
25 {
26  if (m_available_packets.empty())
27  return UDPPacket(0);
28 
29  UDPPacket packet(m_available_packets.front());
30  m_available_packets.pop_front();
31 
32  return packet;
33 }
34 
36 {
37  QMap<uint64_t, UDPPacket>::iterator it = m_empty_packets.begin();
38  if (it == m_empty_packets.end())
39  {
41  }
42 
43  UDPPacket packet(*it);
44  m_empty_packets.erase(it);
45 
46  return packet;
47 }
48 
50 {
51  static constexpr uint64_t k_mask_upper_32 = ~((UINT64_C(1) << (64 - 32)) - 1);
52  uint64_t top = packet.GetKey() & k_mask_upper_32;
53  if (top == (m_next_empty_packet_key & k_mask_upper_32))
54  m_empty_packets[packet.GetKey()] = packet;
55 }
mythrandom.h
PacketBuffer::FreePacket
void FreePacket(const UDPPacket &packet)
Frees an RTPDataPacket returned by PopDataPacket.
Definition: packetbuffer.cpp:49
PacketBuffer::m_next_empty_packet_key
uint64_t m_next_empty_packet_key
Packets key to use for next empty packet.
Definition: packetbuffer.h:49
PacketBuffer::m_available_packets
QList< UDPPacket > m_available_packets
Ordered list of available packets.
Definition: packetbuffer.h:55
PacketBuffer::HasAvailablePacket
bool HasAvailablePacket(void) const
Returns true if there are ordered packets ready for processing.
Definition: packetbuffer.cpp:19
PacketBuffer::m_empty_packets
QMap< uint64_t, UDPPacket > m_empty_packets
Packets ready for reuse.
Definition: packetbuffer.h:52
PacketBuffer::PacketBuffer
PacketBuffer(unsigned int bitrate)
Definition: packetbuffer.cpp:13
PacketBuffer::PopDataPacket
UDPPacket PopDataPacket(void)
Fetches a data packet for processing.
Definition: packetbuffer.cpp:24
UDPPacket
UDP Packet.
Definition: udppacket.h:20
packetbuffer.h
MythRandomStd::MythRandom
uint32_t MythRandom()
generate 32 random bits
Definition: mythrandom.h:20
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:35