JackTrip
Signals | Public Member Functions | Private Slots | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
UdpDataProtocol Class Reference

UDP implementation of DataProtocol class. More...

#include <UdpDataProtocol.h>

Inheritance diagram for UdpDataProtocol:
Inheritance graph
[legend]
Collaboration diagram for UdpDataProtocol:
Collaboration graph
[legend]

Signals

void signalWatingTooLong (int wait_msec)
 Signals when waiting every 10 milliseconds, with the total wait on wait_msec.

Public Member Functions

 UdpDataProtocol (JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port, unsigned int udp_redundancy_factor=1)
 The class constructor.
virtual ~UdpDataProtocol ()
 The class destructor.
void setPeerAddress (const char *peerHostOrIP)
 Set the Peer address to connect to.
virtual int receivePacket (QUdpSocket &UdpSocket, char *buf, const size_t n)
 Receives a packet. It blocks until a packet is received.
virtual int sendPacket (QUdpSocket &UdpSocket, const QHostAddress &PeerAddress, const char *buf, const size_t n)
 Sends a packet.
virtual void getPeerAddressFromFirstPacket (QUdpSocket &UdpSocket, QHostAddress &peerHostAddress, uint16_t &port)
 Obtains the peer address from the first UDP packet received. This address is used by the SERVER mode to connect back to the client.
void setBindPort (int port)
 Sets the bind port number.
void setPeerPort (int port)
 Sets the peer port number.
virtual void run ()
 Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() )
- Public Member Functions inherited from DataProtocol
 DataProtocol (JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port)
 The class constructor.
virtual ~DataProtocol ()
 The class destructor.
virtual void stop ()
 Stops the execution of the Thread.
void setAudioPacketSize (const size_t size_bytes)
 Sets the size of the audio part of the packets.
size_t getAudioPacketSizeInBites ()
 Get the size of the audio part of the packets.

Private Slots

void printUdpWaitedTooLong (int wait_msec)

Private Member Functions

void bindSocket (QUdpSocket &UdpSocket)
 Binds the UDP socket to the available address and specified port.
bool waitForReady (QUdpSocket &UdpSocket, int timeout_msec)
 This function blocks until data is available for reading in the QUdpSocket. The function will timeout after timeout_msec microseconds.
void receivePacketRedundancy (QUdpSocket &UdpSocket, int8_t *full_redundant_packet, int full_redundant_packet_size, int full_packet_size, uint16_t &current_seq_num, uint16_t &last_seq_num, uint16_t &newer_seq_num)
 Redundancy algorythm at the receiving end.
void sendPacketRedundancy (QUdpSocket &UdpSocket, QHostAddress &PeerAddress, int8_t *full_redundant_packet, int full_redundant_packet_size, int full_packet_size)
 Redundancy algorythm at the sender end.

Private Attributes

int mBindPort
 Local Port number to Bind.
int mPeerPort
 Peer Port number.
const runModeT mRunMode
 Run mode, either SENDER or RECEIVER.
QHostAddress mPeerAddress
 The Peer Address.
int8_tmAudioPacket
 Buffer to store Audio Packets.
int8_tmFullPacket
 Buffer to store Full Packet (audio+header)
unsigned int mUdpRedundancyFactor
 Factor of redundancy.

Static Private Attributes

static QMutex sUdpMutex
 Mutex to make thread safe the binding process.

Additional Inherited Members

- Public Types inherited from DataProtocol
enum  packetHeaderTypeT { DEFAULT, JAMLINK, EMPTY }
 Enum to define packet header types. More...
enum  runModeT { SENDER, RECEIVER }
 Enum to define class modes, SENDER or RECEIVER. More...
- Protected Member Functions inherited from DataProtocol
runModeT getRunMode () const
 Get the Run Mode of the object.
- Protected Attributes inherited from DataProtocol
volatile bool mStopped
 Boolean stop the execution of the thread.
volatile bool mHasPeerAddress
 Boolean to indicate if the RECEIVER is waiting to obtain peer address.
volatile bool mHasPacketsToReceive
 Boolean that indicates if a packet was received.
JackTripmJackTrip
 JackTrip mediator class.

Detailed Description

UDP implementation of DataProtocol class.

The class has a bind port and a peer port. The meaning of these depends on the runModeT. If it's a SENDER, bind port is the source port and peer port is the destination port for each UDP packet. If it's a RECEIVER, the bind port destination port (for incoming packets) and the peer port is the source port.

The SENDER and RECEIVER socket can share the same port/address pair (for compatibility with the JamLink boxes). This is achieved setting the resusable property in the socket for address and port. You have to externaly check if the port is already binded if you want to avoid re-binding to the same port.

Constructor & Destructor Documentation

UdpDataProtocol::UdpDataProtocol ( JackTrip jacktrip,
const runModeT  runmode,
int  bind_port,
int  peer_port,
unsigned int  udp_redundancy_factor = 1 
)

The class constructor.

Parameters
jacktripPointer to the JackTrip class that connects all classes (mediator)
runmodeSets the run mode, use either SENDER or RECEIVER
bind_portPort number to bind for this socket (this is the receive or send port depending on the runmode)
peer_portPeer port number (this is the receive or send port depending on the runmode)
udp_redundancy_factorNumber of redundant packets
UdpDataProtocol::~UdpDataProtocol ( )
virtual

The class destructor.

Member Function Documentation

void UdpDataProtocol::bindSocket ( QUdpSocket &  UdpSocket)
private

Binds the UDP socket to the available address and specified port.

void UdpDataProtocol::getPeerAddressFromFirstPacket ( QUdpSocket &  UdpSocket,
QHostAddress &  peerHostAddress,
uint16_t port 
)
virtual

Obtains the peer address from the first UDP packet received. This address is used by the SERVER mode to connect back to the client.

Parameters
peerHostAddressQHostAddress to store the peer address
portReceiving port
void UdpDataProtocol::printUdpWaitedTooLong ( int  wait_msec)
privateslot
int UdpDataProtocol::receivePacket ( QUdpSocket &  UdpSocket,
char *  buf,
const size_t  n 
)
virtual

Receives a packet. It blocks until a packet is received.

This function makes sure we recieve a complete packet of size n

Parameters
bufBuffer to store the recieved packet
nsize of packet to receive
Returns
number of bytes read, -1 on error
void UdpDataProtocol::receivePacketRedundancy ( QUdpSocket &  UdpSocket,
int8_t full_redundant_packet,
int  full_redundant_packet_size,
int  full_packet_size,
uint16_t current_seq_num,
uint16_t last_seq_num,
uint16_t newer_seq_num 
)
private

Redundancy algorythm at the receiving end.

void UdpDataProtocol::run ( )
virtual

Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() )

This function creats and binds all the socket and start the connection loop thread.

Implements DataProtocol.

int UdpDataProtocol::sendPacket ( QUdpSocket &  UdpSocket,
const QHostAddress &  PeerAddress,
const char *  buf,
const size_t  n 
)
virtual

Sends a packet.

This function meakes sure we send a complete packet of size n

Parameters
bufBuffer to send
nsize of packet to receive
Returns
number of bytes read, -1 on error
void UdpDataProtocol::sendPacketRedundancy ( QUdpSocket &  UdpSocket,
QHostAddress &  PeerAddress,
int8_t full_redundant_packet,
int  full_redundant_packet_size,
int  full_packet_size 
)
private

Redundancy algorythm at the sender end.

void UdpDataProtocol::setBindPort ( int  port)
inline

Sets the bind port number.

void UdpDataProtocol::setPeerAddress ( const char *  peerHostOrIP)
virtual

Set the Peer address to connect to.

Parameters
peerHostOrIPIPv4 number or host name

Implements DataProtocol.

void UdpDataProtocol::setPeerPort ( int  port)
inlinevirtual

Sets the peer port number.

Implements DataProtocol.

void UdpDataProtocol::signalWatingTooLong ( int  wait_msec)
signal

Signals when waiting every 10 milliseconds, with the total wait on wait_msec.

Parameters
wait_msecTotal wait in milliseconds
bool UdpDataProtocol::waitForReady ( QUdpSocket &  UdpSocket,
int  timeout_msec 
)
private

This function blocks until data is available for reading in the QUdpSocket. The function will timeout after timeout_msec microseconds.

This function is intended to replace QAbstractSocket::waitForReadyRead which has some problems with multithreading.

Returns
returns true if there is data available for reading; otherwise it returns false (if an error occurred or the operation timed out)

Member Data Documentation

int8_t* UdpDataProtocol::mAudioPacket
private

Buffer to store Audio Packets.

int UdpDataProtocol::mBindPort
private

Local Port number to Bind.

int8_t* UdpDataProtocol::mFullPacket
private

Buffer to store Full Packet (audio+header)

QHostAddress UdpDataProtocol::mPeerAddress
private

The Peer Address.

int UdpDataProtocol::mPeerPort
private

Peer Port number.

const runModeT UdpDataProtocol::mRunMode
private

Run mode, either SENDER or RECEIVER.

unsigned int UdpDataProtocol::mUdpRedundancyFactor
private

Factor of redundancy.

QMutex UdpDataProtocol::sUdpMutex
staticprivate

Mutex to make thread safe the binding process.


The documentation for this class was generated from the following files: