JackTrip
JackTrip.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 __JACKTRIP_H__
39 #define __JACKTRIP_H__
40 
41 #include <tr1/memory> //for shared_ptr
42 
43 #include <QObject>
44 #include <QString>
45 
46 #include "DataProtocol.h"
47 #include "JackAudioInterface.h"
48 #include "PacketHeader.h"
49 #include "RingBuffer.h"
50 
51 
59 class JackTrip : public QThread
60 {
61  Q_OBJECT;
62 
63 public:
64 
65  //----------ENUMS------------------------------------------
68  UDP,
69  TCP,
71  };
72 
78  };
79 
84  };
85  //---------------------------------------------------------
86 
87 
96  JackTrip(jacktripModeT JacktripMode = CLIENT,
97  dataProtocolT DataProtocolType = UDP,
98  int NumChans = 2,
99  int BufferQueueLength = 8,
100  unsigned int redundancy = 1,
101  JackAudioInterface::audioBitResolutionT AudioBitResolution =
103  DataProtocol::packetHeaderTypeT PacketHeaderType =
105  underrunModeT UnderRunMode = WAVETABLE,
106  int receiver_bind_port = gDefaultPort,
107  int sender_bind_port = gDefaultPort,
108  int receiver_peer_port = gDefaultPort,
109  int sender_peer_port = gDefaultPort);
110 
112  virtual ~JackTrip();
113 
115  void setPeerAddress(const char* PeerHostOrIP);
116 
120  //void appendProcessPlugin(const std::tr1::shared_ptr<ProcessPlugin> plugin);
121  void appendProcessPlugin(ProcessPlugin* plugin);
122 
124  void start();
125 
127  void stop();
128 
131  void wait();
132 
135  void checkIfPortIsBinded(int port);
136 
137  //------------------------------------------------------------------------------------
139 
140  //
142  void setJackTripMode(jacktripModeT JacktripMode)
143  { mJackTripMode = JacktripMode; }
145  void setDataProtocoType(dataProtocolT DataProtocolType)
146  { mDataProtocol = DataProtocolType; }
149  {
150  mPacketHeaderType = PacketHeaderType;
151  delete mPacketHeader;
152  mPacketHeader = NULL;
154  }
157  //void setNumChannels(int NumChans)
158  //{ mNumChans=NumChans; }
160  void setBufferQueueLength(int BufferQueueLength)
161  { mBufferQueueLength = BufferQueueLength; }
164  { mAudioBitResolution = AudioBitResolution; }
166  void setUnderRunMode(underrunModeT UnderRunMode)
167  { mUnderRunMode = UnderRunMode; }
170  void setAllPorts(int port)
171  {
172  mReceiverBindPort = port;
173  mSenderPeerPort = port;
174  mSenderBindPort = port;
175  mReceiverPeerPort = port;
176  }
178  void setBindPorts(int port)
179  {
180  mReceiverBindPort = port;
181  mSenderBindPort = port;
182  }
184  void setPeerPorts(int port)
185  {
186  mSenderPeerPort = port;
187  mReceiverPeerPort = port;
188  }
190  void setClientName(char* ClientName)
191  { mJackClientName = ClientName; }
193  //------------------------------------------------------------------------------------
194 
195 
196  //------------------------------------------------------------------------------------
198 
199 
200  void createHeader(const DataProtocol::packetHeaderTypeT headertype);
201  void putHeaderInPacket(int8_t* full_packet, int8_t* audio_packet);
202  int getPacketSizeInBytes() const;
203  void parseAudioPacket(int8_t* full_packet, int8_t* audio_packet);
204  void sendNetworkPacket(const int8_t* ptrToSlot)
205  { mSendRingBuffer->insertSlotNonBlocking(ptrToSlot); }
206  void receiveNetworkPacket(int8_t* ptrToReadSlot)
207  { mReceiveRingBuffer->readSlotNonBlocking(ptrToReadSlot); }
208  void readAudioBuffer(int8_t* ptrToReadSlot)
209  { mSendRingBuffer->readSlotBlocking(ptrToReadSlot); }
210  void writeAudioBuffer(const int8_t* ptrToSlot)
213  { return mJackAudio->getBufferSizeInSamples(); }
215  { return mJackAudio->getSampleRateType(); }
217  { return mJackAudio->getAudioBitResolution(); }
219  { return mJackAudio->getNumInputChannels(); }
221  {return mJackAudio->getNumOutputChannels(); }
222  void checkPeerSettings(int8_t* full_packet);
225  int getSequenceNumber() const
226  { return mPacketHeader->getSequenceNumber(); }
227  int getPeerSequenceNumber(int8_t* full_packet) const
228  { return mPacketHeader->getPeerSequenceNumber(full_packet); }
230  //------------------------------------------------------------------------------------
231 
232 
233 public slots:
236  {
237  std::cout << "Stopping JackTrip..." << std::endl;
238  stop();
239  };
240 
246  void slotUdpWatingTooLong(int wait_msec)
247  {
248  int wait_time = 30000; // msec
249  if ( !(wait_msec%wait_time) ) {
250  std::cerr << "UDP WAITED MORE THAN 30 seconds." << std::endl;
252  }
253  }
254 
255 
256 signals:
258  void signalProcessesStopped();
261 
262 
263 private:
264 
266  void setupJackAudio();
268  void setupDataProtocol();
270  void setupRingBuffers();
272  void clientStart();
274  void serverStart();
277 
281 
282  int mNumChans;
287  QString mPeerAddress;
288 
296 
301 
306 
307  unsigned int mRedundancy;
308  const char* mJackClientName;
309 
310  QVector<ProcessPlugin*> mProcessPlugins;
311 };
312 
313 #endif