JackTrip
UdpMasterListener.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 __UDPMASTERLISTENER_H__
39 #define __UDPMASTERLISTENER_H__
40 
41 #include <iostream>
42 #include <stdexcept>
43 
44 #include <QThread>
45 #include <QThreadPool>
46 #include <QUdpSocket>
47 #include <QHostAddress>
48 #include <QTcpSocket>
49 #include <QTcpServer>
50 #include <QMutex>
51 
52 #include "JackTrip.h"
53 #include "jacktrip_types.h"
54 #include "jacktrip_globals.h"
55 class JackTripWorker; // forward declaration
56 
57 typedef struct {
58  QString address;
61 
67 class UdpMasterListener : public QThread
68 {
69  Q_OBJECT;
70 
71 public:
72  UdpMasterListener(int server_port = gServerUdpPort);
73  virtual ~UdpMasterListener();
74 
77  void run();
78 
80  void stop() { mStopped = true; }
81 
82  int releaseThread(int id);
83 
84  void setConnectDefaultAudioPorts(bool connectDefaultAudioPorts) { m_connectDefaultAudioPorts = connectDefaultAudioPorts; }
85 
86 private slots:
87  void testReceive()
88  { std::cout << "========= TEST RECEIVE SLOT ===========" << std::endl; }
89 
90 signals:
91  void Listening();
92  void ClientAddressSet();
93  void signalRemoveThread(int id);
94 
95 
96 private:
101  static void bindUdpSocket(QUdpSocket& udpsocket, int port) throw(std::runtime_error);
102 
103  int readClientUdpPort(QTcpSocket* clientConnection);
104  int sendUdpPort(QTcpSocket* clientConnection, int udp_port);
105 
106 
111  //void sendToPoolPrototype(int id);
112 
117  int isNewAddress(QString address, uint16_t port);
118 
122  int getPoolID(QString address, uint16_t port);
123 
124  //QUdpSocket mUdpMasterSocket; ///< The UDP socket
125  //QHostAddress mPeerAddress; ///< The Peer Address
126 
127  //JackTripWorker* mJTWorker; ///< Class that will be used as prototype
128  QVector<JackTripWorker*>* mJTWorkers;
129  QThreadPool mThreadPool;
130 
131  int mServerPort; //< Server known port number
132  int mBasePort;
133  addressPortPair mActiveAddress[gMaxThreads];
134  QHash<QString, uint16_t> mActiveAddressPortPair;
135 
137  volatile bool mStopped;
138  int mTotalRunningThreads;
139  QMutex mMutex;
140  JackTrip::underrunModeT mUnderRunMode;
141  int mBufferQueueLength;
142 
143  bool m_connectDefaultAudioPorts;
144 
145 #ifdef WAIR // wair
146  bool mWAIR;
147  void connectMesh(bool spawn);
148  void enumerateRunningThreadIDs();
149 public :
150  void setWAIR(int b) {mWAIR = b;}
151  bool isWAIR() {return mWAIR;}
152 #endif // endwhere
153  void connectPatch(bool spawn);
154 public :
155  unsigned int mHubPatch;
156  void setHubPatch(unsigned int p) {mHubPatch = p;}
157  unsigned int getHubPatch() {return mHubPatch;}
158 
159  void setUnderRunMode(JackTrip::underrunModeT UnderRunMode) { mUnderRunMode = UnderRunMode; }
160  void setBufferQueueLength(int BufferQueueLength) { mBufferQueueLength = BufferQueueLength; }
161 };
162 
163 
164 #endif //__UDPMASTERLISTENER_H__
UdpMasterListener::setConnectDefaultAudioPorts
void setConnectDefaultAudioPorts(bool connectDefaultAudioPorts)
Definition: UdpMasterListener.h:84
UdpMasterListener::ClientAddressSet
void ClientAddressSet()
Definition: moc_UdpMasterListener.cpp:166
uint16_t
quint16 uint16_t
Typedef for unsigned short. This type is guaranteed to be 16-bit.
Definition: jacktrip_types.h:71
addressPortPair::address
QString address
Definition: UdpMasterListener.h:58
UdpMasterListener::setBufferQueueLength
void setBufferQueueLength(int BufferQueueLength)
Definition: UdpMasterListener.h:160
UdpMasterListener::getHubPatch
unsigned int getHubPatch()
Definition: UdpMasterListener.h:157
UdpMasterListener::setHubPatch
void setHubPatch(unsigned int p)
Definition: UdpMasterListener.h:156
JackTripWorker
Prototype of the worker class that will be cloned through sending threads to the Thread Pool.
Definition: JackTripWorker.h:67
UdpMasterListener::signalRemoveThread
void signalRemoveThread(int id)
Definition: moc_UdpMasterListener.cpp:172
UdpMasterListener::releaseThread
int releaseThread(int id)
Definition: UdpMasterListener.cpp:421
UdpMasterListener::UdpMasterListener
UdpMasterListener(int server_port=gServerUdpPort)
Definition: UdpMasterListener.cpp:56
addressPortPair::port
int16_t port
Definition: UdpMasterListener.h:59
JackTrip::underrunModeT
underrunModeT
Enum for the JackTrip Underrun Mode, when packets.
Definition: JackTrip.h:90
UdpMasterListener::run
void run()
Implements the Thread Loop. To start the thread, call start() ( DO NOT CALL run() )
Definition: UdpMasterListener.cpp:111
gServerUdpPort
const int gServerUdpPort
Public well-known UDP port to where the clients will connect.
Definition: jacktrip_globals.h:141
gMaxThreads
const int gMaxThreads
Maximum Threads that can be run at the same time.
Definition: jacktrip_globals.h:138
UdpMasterListener::stop
void stop()
Stops the execution of the Thread.
Definition: UdpMasterListener.h:80
jacktrip_globals.h
UdpMasterListener
Master UDP listener on the Server.
Definition: UdpMasterListener.h:67
UdpMasterListener::mHubPatch
unsigned int mHubPatch
Definition: UdpMasterListener.h:155
UdpMasterListener::setUnderRunMode
void setUnderRunMode(JackTrip::underrunModeT UnderRunMode)
Definition: UdpMasterListener.h:159
JackTrip.h
addressPortPair
Definition: UdpMasterListener.h:57
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
UdpMasterListener::~UdpMasterListener
virtual ~UdpMasterListener()
Definition: UdpMasterListener.cpp:94
UdpMasterListener::Listening
void Listening()
Definition: moc_UdpMasterListener.cpp:160