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------------------------------------------
105  };
106 
108  enum runModeT {
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 signals:
175 
176  void signalError(const char* error_message);
178 
179 
180 protected:
181 
185  runModeT getRunMode() const { return mRunMode; }
186 
188  volatile bool mStopped;
190  volatile bool mHasPeerAddress;
192  volatile bool mHasPacketsToReceive;
193  QMutex mMutex;
194 
195 
196 private:
197 
198  int mLocalPort;
199  int mPeerPort;
200  const runModeT mRunMode;
201 
202  struct sockaddr_in mLocalIPv4Addr;
203  struct sockaddr_in mPeerIPv4Addr;
204 
207  static int sClientsRunning;
208 
209  size_t mAudioPacketSize;
210 
211 
213 protected:
214  //PacketHeader* mHeader; ///< Packet Header
216 
217 };
218 
219 #endif
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:93
DataProtocol::mStopped
volatile bool mStopped
Boolean stop the execution of the thread.
Definition: DataProtocol.h:188
DataProtocol::SENDER
@ SENDER
Set class as a Sender (send packets)
Definition: DataProtocol.h:109
DataProtocol::mMutex
QMutex mMutex
Definition: DataProtocol.h:193
DataProtocol::mHasPeerAddress
volatile bool mHasPeerAddress
Boolean to indicate if the RECEIVER is waiting to obtain peer address.
Definition: DataProtocol.h:190
JackTrip
Main class to creates a SERVER (to listen) or a CLIENT (to connect to a listening server) to send aud...
Definition: JackTrip.h:67
DataProtocol::setPeerAddress
virtual void setPeerAddress(const char *peerHostOrIP)=0
Set the peer address.
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:185
DataProtocol::DataProtocol
DataProtocol(JackTrip *jacktrip, const runModeT runmode, int bind_port, int peer_port)
The class constructor.
Definition: DataProtocol.cpp:52
DataProtocol::setSocket
virtual void setSocket(int &socket)=0
DataProtocol::stop
virtual void stop()
Stops the execution of the Thread.
Definition: DataProtocol.h:138
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:192
DataProtocol::DEFAULT
@ DEFAULT
Default application header.
Definition: DataProtocol.h:102
DataProtocol::mJackTrip
JackTrip * mJackTrip
JackTrip mediator class.
Definition: DataProtocol.h:215
DataProtocol::RECEIVER
@ RECEIVER
Set class as a Receiver (receives packets)
Definition: DataProtocol.h:110
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