JackTrip
DataProtocol.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 __DATAPROTOCOL_H__
39 #define __DATAPROTOCOL_H__
40 
41 #ifdef __WIN_32__
42 #include <winsock2.h>
43 #include <ws2tcpip.h>
44 #endif
45 
46 #ifndef __WIN_32__
47 #include <netinet/in.h> //sockaddr_in{} and other Internet defns
48 #include <arpa/inet.h> //inet(3) functions
49 #include <netdb.h>
50 //#include <tr1/memory> //for shared_ptr
51 #endif
52 
53 #include <iostream>
54 
55 #include <QThread>
56 #include <QHostAddress>
57 #include <QMutex>
58 #include <QMutexLocker>
59 
60 class JackTrip; // forward declaration
61 
62 
93 class DataProtocol : public QThread
94 {
95  Q_OBJECT;
96 
97 public:
98 
99  //----------ENUMS------------------------------------------
104  EMPTY
105  };
106 
108  enum runModeT {
110  RECEIVER
111  };
112  //---------------------------------------------------------
113 
114 
123  DataProtocol(JackTrip* jacktrip,
124  const runModeT runmode,
125  int bind_port, int peer_port);
126 
128  virtual ~DataProtocol();
129 
135  virtual void run() = 0;
136 
138  virtual void stop() {
139  QMutexLocker lock(&mMutex);
140  mStopped = true;
141  }
142 
146  void setAudioPacketSize(const size_t size_bytes){ mAudioPacketSize = size_bytes; }
147 
151  size_t getAudioPacketSizeInBites() { return(mAudioPacketSize); }
152 
157  virtual void setPeerAddress(const char* peerHostOrIP) = 0;
158 
163  virtual void setPeerPort(int port) = 0;
164 
165  //virtual void getPeerAddressFromFirstPacket(QHostAddress& peerHostAddress,
166  // uint16_t& port) = 0;
167 
168 #if defined (__WIN_32__)
169  virtual void setSocket(SOCKET &socket) = 0;
170 #else
171  virtual void setSocket(int &socket) = 0;
172 #endif
173 
174  struct PktStat {
180  };
181  virtual bool getStats(PktStat*) {return false;}
182 
183 signals:
184 
185  void signalError(const char* error_message);
187 
188 
189 protected:
190 
194  runModeT getRunMode() const { return mRunMode; }
195 
197  volatile bool mStopped;
199  volatile bool mHasPeerAddress;
201  volatile bool mHasPacketsToReceive;
202  QMutex mMutex;
203 
204 
205 private:
206 
207  int mLocalPort;
208  int mPeerPort;
209  const runModeT mRunMode;
210 
211  struct sockaddr_in mLocalIPv4Addr;
212  struct sockaddr_in mPeerIPv4Addr;
213 
216  static int sClientsRunning;
217 
218  size_t mAudioPacketSize;
219 
220 
222 protected:
223  //PacketHeader* mHeader; ///< Packet Header
225 
226 };
227 
228 #endif
DataProtocol::PktStat
Definition: DataProtocol.h:174
DataProtocol::run
virtual void run()=0
Implements the thread loop.
DataProtocol::signalReceivedConnectionFromPeer
void signalReceivedConnectionFromPeer()
Definition: moc_DataProtocol.cpp:149
DataProtocol::~DataProtocol
virtual ~DataProtocol()
The class destructor.
Definition: DataProtocol.cpp:60
DataProtocol
Base class that defines the transmission protocol.
Definition: DataProtocol.h:94
DataProtocol::mStopped
volatile bool mStopped
Boolean stop the execution of the thread.
Definition: DataProtocol.h:197
DataProtocol::SENDER
@ SENDER
Set class as a Sender (send packets)
Definition: DataProtocol.h:109
DataProtocol::mMutex
QMutex mMutex
Definition: DataProtocol.h:202
DataProtocol::mHasPeerAddress
volatile bool mHasPeerAddress
Boolean to indicate if the RECEIVER is waiting to obtain peer address.
Definition: DataProtocol.h:199
JackTrip
Main class to creates a SERVER (to listen) or a CLIENT (to connect to a listening server) to send aud...
Definition: JackTrip.h:68
DataProtocol::PktStat::statCount
uint32_t statCount
Definition: DataProtocol.h:179
DataProtocol::setPeerAddress
virtual void setPeerAddress(const char *peerHostOrIP)=0
Set the peer address.
DataProtocol::PktStat::outOfOrder
uint32_t outOfOrder
Definition: DataProtocol.h:177
DataProtocol::JAMLINK
@ JAMLINK
Header to use with Jamlinks.
Definition: DataProtocol.h:103
DataProtocol::setPeerPort
virtual void setPeerPort(int port)=0
Set the peer incomming (receiving) port number.
DataProtocol::getRunMode
runModeT getRunMode() const
Get the Run Mode of the object.
Definition: DataProtocol.h:194
DataProtocol::DataProtocol
DataProtocol(JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port)
The class constructor.
Definition: DataProtocol.cpp:52
DataProtocol::PktStat::tot
uint32_t tot
Definition: DataProtocol.h:175
DataProtocol::PktStat::lost
uint32_t lost
Definition: DataProtocol.h:176
DataProtocol::setSocket
virtual void setSocket(int &socket)=0
DataProtocol::stop
virtual void stop()
Stops the execution of the Thread.
Definition: DataProtocol.h:138
uint32_t
quint32 uint32_t
Typedef for unsigned int. This type is guaranteed to be 32-bit.
Definition: jacktrip_types.h:73
DataProtocol::setAudioPacketSize
void setAudioPacketSize(const size_t size_bytes)
Sets the size of the audio part of the packets.
Definition: DataProtocol.h:146
DataProtocol::signalError
void signalError(const char *error_message)
Definition: moc_DataProtocol.cpp:142
DataProtocol::runModeT
runModeT
Enum to define class modes, SENDER or RECEIVER.
Definition: DataProtocol.h:108
DataProtocol::mHasPacketsToReceive
volatile bool mHasPacketsToReceive
Boolean that indicates if a packet was received.
Definition: DataProtocol.h:201
DataProtocol::DEFAULT
@ DEFAULT
Default application header.
Definition: DataProtocol.h:102
DataProtocol::mJackTrip
JackTrip * mJackTrip
JackTrip mediator class.
Definition: DataProtocol.h:224
DataProtocol::RECEIVER
@ RECEIVER
Set class as a Receiver (receives packets)
Definition: DataProtocol.h:110
DataProtocol::getStats
virtual bool getStats(PktStat *)
Definition: DataProtocol.h:181
DataProtocol::getAudioPacketSizeInBites
size_t getAudioPacketSizeInBites()
Get the size of the audio part of the packets.
Definition: DataProtocol.h:151
DataProtocol::packetHeaderTypeT
packetHeaderTypeT
Enum to define packet header types.
Definition: DataProtocol.h:101
DataProtocol::EMPTY
@ EMPTY
Empty Header.
Definition: DataProtocol.h:104
DataProtocol::PktStat::revived
uint32_t revived
Definition: DataProtocol.h:178