JackTrip
UdpHubListener.h
Go to the documentation of this file.
1 //*****************************************************************
2 /*
3  JackTrip: A System for High-Quality Audio Network Performance
4  over the Internet
5 
6  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
7  SoundWIRE group at CCRMA, Stanford University.
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  OTHER DEALINGS IN THE SOFTWARE.
29 */
30 //*****************************************************************
31 
38 #ifndef __UDPHUBLISTENER_H__
39 #define __UDPHUBLISTENER_H__
40 
41 #include <iostream>
42 #include <stdexcept>
43 #include <fstream>
44 
45 #include <QThread>
46 #include <QThreadPool>
47 #include <QUdpSocket>
48 #include <QHostAddress>
49 #include <QTcpSocket>
50 #include <QTcpServer>
51 #include <QMutex>
52 
53 #include "JackTrip.h"
54 #include "jacktrip_types.h"
55 #include "jacktrip_globals.h"
56 class JackTripWorker; // forward declaration
57 class Settings;
58 
59 typedef struct {
60  QString address;
63 
69 class UdpHubListener : public QObject
70 {
71  Q_OBJECT;
72 
73 public:
74  UdpHubListener(int server_port = gServerUdpPort, int server_udp_port = 0);
75  virtual ~UdpHubListener();
76 
78  void start();
79 
81  void stop() { mStopped = true; }
82 
83  int releaseThread(int id);
84 
85  void setConnectDefaultAudioPorts(bool connectDefaultAudioPorts) { m_connectDefaultAudioPorts = connectDefaultAudioPorts; }
86 
87  static void sigIntHandler(__attribute__((unused)) int unused)
88  { std::cout << std::endl << "Shutting Down..." << std::endl; sSigInt = true; }
89 
90 private slots:
91  void testReceive()
92  { std::cout << "========= TEST RECEIVE SLOT ===========" << std::endl; }
93  void receivedNewConnection();
94  void stopCheck();
95 
96 signals:
97  void Listening();
99  void signalRemoveThread(int id);
101  void signalError(const QString &errorMessage);
102 
103 private:
108  void receivedClientInfo(QTcpSocket *clientConnection);
109 
110  static void bindUdpSocket(QUdpSocket& udpsocket, int port);
111 
112  uint16_t readClientUdpPort(QTcpSocket* clientConnection, QString &clientName);
113  int sendUdpPort(QTcpSocket* clientConnection, int udp_port);
114 
115 
120  //void sendToPoolPrototype(int id);
121 
126  int isNewAddress(QString address, uint16_t port);
127 
131  int getPoolID(QString address, uint16_t port);
132 
133  void stopAllThreads();
134 
135  //QUdpSocket mUdpHubSocket; ///< The UDP socket
136  //QHostAddress mPeerAddress; ///< The Peer Address
137 
138  //JackTripWorker* mJTWorker; ///< Class that will be used as prototype
139  QVector<JackTripWorker*>* mJTWorkers;
140  QThreadPool mThreadPool;
141 
142  QTcpServer mTcpServer;
143  int mServerPort; //< Server known port number
144  int mServerUdpPort; //< Server udp base port number
145  int mBasePort;
146  addressPortPair mActiveAddress[gMaxThreads];
147  QHash<QString, uint16_t> mActiveAddressPortPair;
148 
150  volatile bool mStopped;
151  static bool sSigInt;
152  QTimer mStopCheckTimer;
153  int mTotalRunningThreads;
154  QMutex mMutex;
155  JackTrip::underrunModeT mUnderRunMode;
156  int mBufferQueueLength;
157 
158  QStringList mHubPatchDescriptions;
159  bool m_connectDefaultAudioPorts;
160 
161  int mIOStatTimeout;
162  QSharedPointer<std::ofstream> mIOStatStream;
163 
164  int mBufferStrategy;
165  int mBroadcastQueue;
166  double mSimulatedLossRate;
167  double mSimulatedJitterRate;
168  double mSimulatedDelayRel;
169  bool mUseRtUdpPriority;
170 
171 #ifdef WAIR // wair
172  bool mWAIR;
173  void connectMesh(bool spawn);
174  void enumerateRunningThreadIDs();
175 public :
176  void setWAIR(int b) {mWAIR = b;}
177  bool isWAIR() {return mWAIR;}
178 #endif // endwhere
179  void connectPatch(bool spawn);
180 public :
181  unsigned int mHubPatch;
182  void setHubPatch(unsigned int p) {mHubPatch = p;}
183  unsigned int getHubPatch() {return mHubPatch;}
184 
185  void setUnderRunMode(JackTrip::underrunModeT UnderRunMode) { mUnderRunMode = UnderRunMode; }
186  void setBufferQueueLength(int BufferQueueLength) { mBufferQueueLength = BufferQueueLength; }
187 
188  void setIOStatTimeout(int timeout) { mIOStatTimeout = timeout; }
189  void setIOStatStream(QSharedPointer<std::ofstream> statStream) { mIOStatStream = statStream; }
190 
191  void setBufferStrategy(int BufferStrategy) { mBufferStrategy = BufferStrategy; }
192  void setNetIssuesSimulation(double loss, double jitter, double delay_rel)
193  {
194  mSimulatedLossRate = loss;
195  mSimulatedJitterRate = jitter;
196  mSimulatedDelayRel = delay_rel;
197  }
198  void setBroadcast(int broadcast_queue) {mBroadcastQueue = broadcast_queue;}
199  void setUseRtUdpPriority(bool use) {mUseRtUdpPriority = use;}
200 
201 };
202 
203 
204 #endif //__UDPHUBLISTENER_H__
UdpHubListener::getHubPatch
unsigned int getHubPatch()
Definition: UdpHubListener.h:183
UdpHubListener::UdpHubListener
UdpHubListener(int server_port=gServerUdpPort, int server_udp_port=0)
Definition: UdpHubListener.cpp:57
UdpHubListener::ClientAddressSet
void ClientAddressSet()
UdpHubListener::stop
void stop()
Stops the execution of the Thread.
Definition: UdpHubListener.h:81
UdpHubListener::mHubPatch
unsigned int mHubPatch
Definition: UdpHubListener.h:181
uint16_t
quint16 uint16_t
Typedef for unsigned short. This type is guaranteed to be 16-bit.
Definition: jacktrip_types.h:71
UdpHubListener::setIOStatTimeout
void setIOStatTimeout(int timeout)
Definition: UdpHubListener.h:188
UdpHubListener
Hub UDP listener on the Server.
Definition: UdpHubListener.h:70
addressPortPair::address
QString address
Definition: UdpHubListener.h:60
UdpHubListener::setConnectDefaultAudioPorts
void setConnectDefaultAudioPorts(bool connectDefaultAudioPorts)
Definition: UdpHubListener.h:85
UdpHubListener::signalStopped
void signalStopped()
UdpHubListener::setIOStatStream
void setIOStatStream(QSharedPointer< std::ofstream > statStream)
Definition: UdpHubListener.h:189
UdpHubListener::signalRemoveThread
void signalRemoveThread(int id)
UdpHubListener::setHubPatch
void setHubPatch(unsigned int p)
Definition: UdpHubListener.h:182
Settings
Class to set usage options and parse settings from input.
Definition: Settings.h:61
JackTripWorker
Prototype of the worker class that will be cloned through sending threads to the Thread Pool.
Definition: JackTripWorker.h:68
UdpHubListener::setBroadcast
void setBroadcast(int broadcast_queue)
Definition: UdpHubListener.h:198
UdpHubListener::~UdpHubListener
virtual ~UdpHubListener()
Definition: UdpHubListener.cpp:118
UdpHubListener::releaseThread
int releaseThread(int id)
Definition: UdpHubListener.cpp:472
UdpHubListener::setUnderRunMode
void setUnderRunMode(JackTrip::underrunModeT UnderRunMode)
Definition: UdpHubListener.h:185
addressPortPair::port
int16_t port
Definition: UdpHubListener.h:61
UdpHubListener::signalError
void signalError(const QString &errorMessage)
JackTrip::underrunModeT
underrunModeT
Enum for the JackTrip Underrun Mode, when packets.
Definition: JackTrip.h:94
gServerUdpPort
const int gServerUdpPort
Public well-known UDP port to where the clients will connect.
Definition: jacktrip_globals.h:146
gMaxThreads
const int gMaxThreads
Maximum Threads that can be run at the same time.
Definition: jacktrip_globals.h:143
jacktrip_globals.h
UdpHubListener::setBufferQueueLength
void setBufferQueueLength(int BufferQueueLength)
Definition: UdpHubListener.h:186
UdpHubListener::setUseRtUdpPriority
void setUseRtUdpPriority(bool use)
Definition: UdpHubListener.h:199
JackTrip.h
UdpHubListener::start
void start()
Starts the TCP server.
Definition: UdpHubListener.cpp:135
UdpHubListener::setBufferStrategy
void setBufferStrategy(int BufferStrategy)
Definition: UdpHubListener.h:191
addressPortPair
Definition: UdpHubListener.h:59
UdpHubListener::setNetIssuesSimulation
void setNetIssuesSimulation(double loss, double jitter, double delay_rel)
Definition: UdpHubListener.h:192
jacktrip_types.h
int16_t
qint16 int16_t
Typedef for signed short. This type is guaranteed to be 16-bit.
Definition: jacktrip_types.h:80
UdpHubListener::sigIntHandler
static void sigIntHandler(__attribute__((unused)) int unused)
Definition: UdpHubListener.h:87
UdpHubListener::Listening
void Listening()