Codebase list jacktrip / upstream/1.1_repack src / JackTrip.h
upstream/1.1_repack

Tree @upstream/1.1_repack (Download .tar.gz)

JackTrip.h @upstream/1.1_repackraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//*****************************************************************
/*
  JackTrip: A System for High-Quality Audio Network Performance
  over the Internet

  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
  SoundWIRE group at CCRMA, Stanford University.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/
//*****************************************************************

/**
 * \file JackTrip.h
 * \author Juan-Pablo Caceres
 * \date July 2008
 */

#ifndef __JACKTRIP_H__
#define __JACKTRIP_H__

//#include <tr1/memory> //for shared_ptr
#include <stdexcept>

#include <QObject>
#include <QString>
#include <QUdpSocket>

#include "DataProtocol.h"
#include "AudioInterface.h"

#ifndef __NO_JACK__
#include "JackAudioInterface.h"
#endif //__NO_JACK__

#include "PacketHeader.h"
#include "RingBuffer.h"

#include <signal.h>
/** \brief Main class to creates a SERVER (to listen) or a CLIENT (to connect
 * to a listening server) to send audio streams in the network.
 *
 * All audio and network settings can be set in this class.
 * This class also acts as a Mediator between all the other class.
 * Classes that uses JackTrip methods need to register with it.
 */

class JackTrip : public QThread
{
  Q_OBJECT;

public:

  //----------ENUMS------------------------------------------
  /// \brief Enum for the data Protocol. At this time only UDP is implemented
  enum dataProtocolT {
    UDP, ///< Use UDP (User Datagram Protocol) 
    TCP, ///< <B>NOT IMPLEMENTED</B>: Use TCP (Transmission Control Protocol)
    SCTP ///< <B>NOT IMPLEMENTED</B>: Use SCTP (Stream Control Transmission Protocol)
  };

  /// \brief Enum for the JackTrip mode
  enum jacktripModeT {
    SERVER, ///< Run in Server Mode
    CLIENT,  ///< Run in Client Mode
    CLIENTTOPINGSERVER, ///< Client of the Ping Server Mode
    SERVERPINGSERVER ///< Server of the MultiThreaded JackTrip
  };

  /// \brief Enum for the JackTrip Underrun Mode, when packets
  enum underrunModeT {
    WAVETABLE, ///< Loops on the last received packet
    ZEROS  ///< Set new buffers to zero if there are no new ones
  };

  /// \brief Enum for Audio Interface Mode
  enum audiointerfaceModeT {
    JACK, ///< Jack Mode
    RTAUDIO  ///< RtAudio Mode
  };

  /// \brief Enum for Connection Mode (useful for connections to MultiClient Server)
  enum connectionModeT {
    NORMAL, ///< Normal Mode
    KSTRONG,  ///< Karplus Strong
    JAMTEST  ///< Karplus Strong
  };
  //---------------------------------------------------------


  /** \brief The class Constructor with Default Parameters
   * \param JacktripMode JackTrip::CLIENT or JackTrip::SERVER
   * \param DataProtocolType JackTrip::dataProtocolT
   * \param NumChans Number of Audio Channels (same for inputs and outputs)
   * \param BufferQueueLength Audio Buffer for receiving packets
   * \param AudioBitResolution Audio Sample Resolutions in bits
   * \param redundancy redundancy factor for network data
   */
  JackTrip(jacktripModeT JacktripMode = CLIENT,
	   dataProtocolT DataProtocolType = UDP,
     int NumChans = gDefaultNumInChannels,
     int BufferQueueLength = gDefaultQueueLength,
     unsigned int redundancy = gDefaultRedundancy,
     AudioInterface::audioBitResolutionT AudioBitResolution =
     AudioInterface::BIT16,
	   DataProtocol::packetHeaderTypeT PacketHeaderType = 
	   DataProtocol::DEFAULT,
	   underrunModeT UnderRunMode = WAVETABLE,
     int receiver_bind_port = gDefaultPort,
     int sender_bind_port = gDefaultPort,
     int receiver_peer_port = gDefaultPort,
     int sender_peer_port = gDefaultPort);
  
  /// \brief The class destructor
  virtual ~JackTrip();

  /// \brief Starting point for the thread
  virtual void run() {}

  /// \brief Set the Peer Address for jacktripModeT::CLIENT mode only
  virtual void setPeerAddress(const char* PeerHostOrIP);

  /** \brief Append a process plugin. Processes will be appended in order
   * \param plugin Pointer to ProcessPlugin Class
   */
  //void appendProcessPlugin(const std::tr1::shared_ptr<ProcessPlugin> plugin);
  virtual void appendProcessPlugin(ProcessPlugin* plugin);

  /// \brief Start the processing threads
  virtual void startProcess() throw(std::invalid_argument);

  /// \brief Stop the processing threads
  virtual void stop();

  /// \brief Wait for all the threads to finish. This functions is used when JackTrip is 
  /// run as a thread
  virtual void waitThreads();

  /// \brief Check if UDP port is already binded
  /// \param port Port number
  virtual void checkIfPortIsBinded(int port);
  
  //------------------------------------------------------------------------------------
  /// \name Getters and Setters Methods to change parameters after construction
  //@{
  // 
  /// \brief Sets (override) JackTrip Mode after construction
  virtual void setJackTripMode(jacktripModeT JacktripMode)
  { mJackTripMode = JacktripMode; }
  /// \brief Sets (override) DataProtocol Type after construction
  virtual void setDataProtocoType(dataProtocolT DataProtocolType)
  { mDataProtocol = DataProtocolType; }
  /// \brief Sets the Packet header type
  virtual void setPacketHeaderType(DataProtocol::packetHeaderTypeT PacketHeaderType)
  { 
    mPacketHeaderType = PacketHeaderType;
    delete mPacketHeader;
    mPacketHeader = NULL;
    createHeader(mPacketHeaderType);
  }
  /// \brief Sets (override) Buffer Queue Length Mode after construction
  virtual void setBufferQueueLength(int BufferQueueLength)
  { mBufferQueueLength = BufferQueueLength; }
  /// \brief Sets (override) Audio Bit Resolution after construction
  virtual void setAudioBitResolution(AudioInterface::audioBitResolutionT AudioBitResolution)
  { mAudioBitResolution = AudioBitResolution; }
  /// \brief Sets (override) Underrun Mode
  virtual void setUnderRunMode(underrunModeT UnderRunMode)
  { mUnderRunMode = UnderRunMode; }
  /// \brief Sets port numbers for the local and peer machine.
  /// Receive port is <tt>port</tt>
  virtual void setAllPorts(int port)
  {
    mReceiverBindPort = port;
    mSenderPeerPort = port;
    mSenderBindPort = port;
    mReceiverPeerPort = port;
  }
  /// \brief Sets port numbers to bind in RECEIVER and SENDER sockets.
  virtual void setBindPorts(int port)
  {
    mReceiverBindPort = port;
    mSenderBindPort = port;
  }
  /// \brief Sets port numbers for the peer (remote) machine.
  virtual void setPeerPorts(int port)
  {
    mSenderPeerPort = port;
    mReceiverPeerPort = port;
  }
  /// \brief Set Client Name to something different that the default (JackTrip)
  virtual void setClientName(const char* ClientName)
  { mJackClientName = ClientName; }
  /// \brief Set the number of audio channels
  virtual void setNumChannels(int num_chans)
  { mNumChans = num_chans; }

  virtual int getReceiverBindPort() const
  { return mReceiverBindPort; }
  virtual int getSenderPeerPort() const
  { return mSenderPeerPort; }
  virtual int getSenderBindPort() const
  { return mSenderBindPort; }
  virtual int getReceiverPeerPort() const
  { return mReceiverPeerPort; }

  virtual DataProtocol* getDataProtocolSender() const
  { return mDataProtocolSender; }
  virtual DataProtocol* getDataProtocolReceiver() const
  { return mDataProtocolReceiver; }
  virtual void setDataProtocolSender(DataProtocol* const DataProtocolSender)
  { mDataProtocolSender = DataProtocolSender; }
  virtual void setDataProtocolReceiver(DataProtocol* const DataProtocolReceiver)
  { mDataProtocolReceiver = DataProtocolReceiver; }

  virtual RingBuffer* getSendRingBuffer() const
  { return mSendRingBuffer; }
  virtual RingBuffer* getReceiveRingBuffer() const
  { return mReceiveRingBuffer; }
  virtual void setSendRingBuffer(RingBuffer* const SendRingBuffer)
  { mSendRingBuffer = SendRingBuffer; }
  virtual void setReceiveRingBuffer(RingBuffer* const ReceiveRingBuffer)
  { mReceiveRingBuffer = ReceiveRingBuffer; }

  virtual void setPacketHeader(PacketHeader* const PacketHeader)
  { mPacketHeader = PacketHeader; }

  virtual int getRingBuffersSlotSize()
  { return getTotalAudioPacketSizeInBytes(); }

  virtual void setAudiointerfaceMode(JackTrip::audiointerfaceModeT audiointerface_mode)
  { mAudiointerfaceMode = audiointerface_mode; }
  virtual void setAudioInterface(AudioInterface* const AudioInterface)
  { mAudioInterface = AudioInterface; }


  void setSampleRate(uint32_t sample_rate)
  { mSampleRate = sample_rate; }
  void setAudioBufferSizeInSamples(uint32_t buf_size)
  { mAudioBufferSize = buf_size; }

  JackTrip::connectionModeT getConnectionMode() const
  { return mConnectionMode; }
  void setConnectionMode(JackTrip::connectionModeT connection_mode)
  { mConnectionMode = connection_mode; }

  JackTrip::jacktripModeT getJackTripMode() const
  { return mJackTripMode; }

  QString getPeerAddress() const
  { return mPeerAddress; }

  bool receivedConnectionFromPeer()
  { return mReceivedConnection; }

  bool tcpConnectionError()
  { return mTcpConnectionError; }
  //@}
  //------------------------------------------------------------------------------------


  //------------------------------------------------------------------------------------
  /// \name Mediator Functions
  //@{
  /// \todo Document all these functions
  virtual void createHeader(const DataProtocol::packetHeaderTypeT headertype);
  void putHeaderInPacket(int8_t* full_packet, int8_t* audio_packet);
  virtual int getPacketSizeInBytes();
  void parseAudioPacket(int8_t* full_packet, int8_t* audio_packet);
  virtual void sendNetworkPacket(const int8_t* ptrToSlot)
  { mSendRingBuffer->insertSlotNonBlocking(ptrToSlot); }
  virtual void receiveNetworkPacket(int8_t* ptrToReadSlot)
  { mReceiveRingBuffer->readSlotNonBlocking(ptrToReadSlot); }
  virtual void readAudioBuffer(int8_t* ptrToReadSlot)
  { mSendRingBuffer->readSlotBlocking(ptrToReadSlot); }
  virtual void writeAudioBuffer(const int8_t* ptrToSlot)
  { mReceiveRingBuffer->insertSlotNonBlocking(ptrToSlot); }
  uint32_t getBufferSizeInSamples() const
  { return mAudioBufferSize; /*return mAudioInterface->getBufferSizeInSamples();*/ }

  AudioInterface::samplingRateT getSampleRateType() const
  { return mAudioInterface->getSampleRateType(); }
  int getSampleRate() const
  { return mSampleRate; /*return mAudioInterface->getSampleRate();*/ }

  uint8_t getAudioBitResolution() const
  { return mAudioBitResolution*8; /*return mAudioInterface->getAudioBitResolution();*/ }
  unsigned int getNumInputChannels() const
  { return mNumChans; /*return mAudioInterface->getNumInputChannels();*/ }
  unsigned int getNumOutputChannels() const
  { return mNumChans; /*return mAudioInterface->getNumOutputChannels();*/ }
  unsigned int getNumChannels() const
  {
    if (getNumInputChannels() == getNumOutputChannels())
    { return getNumInputChannels(); }
    else { return 0; }
  }
  virtual void checkPeerSettings(int8_t* full_packet);
  void increaseSequenceNumber()
  { mPacketHeader->increaseSequenceNumber(); }
  int getSequenceNumber() const
  { return mPacketHeader->getSequenceNumber(); }

  uint64_t getPeerTimeStamp(int8_t* full_packet) const
  { return mPacketHeader->getPeerTimeStamp(full_packet); }

  uint16_t getPeerSequenceNumber(int8_t* full_packet) const
  { return mPacketHeader->getPeerSequenceNumber(full_packet); }

  uint16_t getPeerBufferSize(int8_t* full_packet) const
  { return mPacketHeader->getPeerBufferSize(full_packet); }

  uint8_t getPeerSamplingRate(int8_t* full_packet) const
  { return mPacketHeader->getPeerSamplingRate(full_packet); }

  uint8_t getPeerBitResolution(int8_t* full_packet) const
  { return mPacketHeader->getPeerBitResolution(full_packet); }

  uint8_t  getPeerNumChannels(int8_t* full_packet) const
  { return mPacketHeader->getPeerNumChannels(full_packet); }

  uint8_t  getPeerConnectionMode(int8_t* full_packet) const
  { return mPacketHeader->getPeerConnectionMode(full_packet); }

  size_t getSizeInBytesPerChannel() const
  { return mAudioInterface->getSizeInBytesPerChannel(); }
  int getHeaderSizeInBytes() const
  { return mPacketHeader->getHeaderSizeInBytes(); }
  virtual int getTotalAudioPacketSizeInBytes() const
  { return mAudioInterface->getSizeInBytesPerChannel() * mNumChans; }
  //@}
  //------------------------------------------------------------------------------------

  void printTextTest() {std::cout << "=== JackTrip PRINT ===" << std::endl;}
  void printTextTest2() {std::cout << "=== JackTrip PRINT2 ===" << std::endl;}

public slots:
  /// \brief Slot to stop all the processes and threads
  virtual void slotStopProcesses()
  {
    std::cout << "Stopping JackTrip..." << std::endl;
    mStopped = true;
    this->stop();
  }

  /** \brief This slot emits in turn the signal signalNoUdpPacketsForSeconds
   * when UDP has waited for more than 30 seconds.
   * 
   * It is used to remove the thread from the server.
   */
  void slotUdpWatingTooLong(int wait_msec)
  { 
    int wait_time = 30000; // msec
    if ( !(wait_msec%wait_time) ) {
      std::cerr << "UDP WAITED MORE THAN 30 seconds." << std::endl;
      emit signalNoUdpPacketsForSeconds();
    }
  }
  void slotPrintTest()
  { std::cout << "=== TESTING ===" << std::endl; }
  void slotReceivedConnectionFromPeer()
  { mReceivedConnection = true; }


signals:

  void signalUdpTimeOut();
  /// \brief Signal emitted when all the processes and threads are stopped
  void signalProcessesStopped();
  /// \brief Signal emitted when no UDP Packets have been received for a while
  void signalNoUdpPacketsForSeconds();
  void signalTcpClientConnected();


public:

  /// \brief Set the AudioInteface object
  virtual void setupAudio();
  /// \brief Close the JackAudioInteface and disconnects it from JACK
  void closeAudio();
  /// \brief Set the DataProtocol objects
  virtual void setupDataProtocol();
  /// \brief Set the RingBuffer objects
  void setupRingBuffers();
  /// \brief Starts for the CLIENT mode
  void clientStart() throw(std::invalid_argument);
  /// \brief Starts for the SERVER mode
  /// \param timout Set the server to timeout after 2 seconds if no client connections are received.
  /// Usefull for the multithreaded server
  /// \return 0 on success, -1 on error
  int serverStart(bool timeout = false, int udpTimeout = gTimeOutMultiThreadedServer)
      throw(std::invalid_argument, std::runtime_error);
  /// \brief Stats for the Client to Ping Server
  /// \return -1 on error, 0 on success
  virtual int clientPingToServerStart() throw(std::invalid_argument);

private:
  //void bindReceiveSocket(QUdpSocket& UdpSocket, int bind_port,
  //                       QHostAddress PeerHostAddress, int peer_port)
  //throw(std::runtime_error);


  jacktripModeT mJackTripMode; ///< JackTrip::jacktripModeT
  dataProtocolT mDataProtocol; ///< Data Protocol Tipe
  DataProtocol::packetHeaderTypeT mPacketHeaderType; ///< Packet Header Type
  JackTrip::audiointerfaceModeT mAudiointerfaceMode;

  int mNumChans; ///< Number of Channels (inputs = outputs)
  int mBufferQueueLength; ///< Audio Buffer from network queue length
  uint32_t mSampleRate; ///< Sample Rate
  uint32_t mAudioBufferSize; ///< Audio buffer size to process on each callback
  AudioInterface::audioBitResolutionT mAudioBitResolution; ///< Audio Bit Resolutions
  QString mPeerAddress; ///< Peer Address to use in jacktripModeT::CLIENT Mode

  /// Pointer to Abstract Type DataProtocol that sends packets
  DataProtocol* mDataProtocolSender;
  /// Pointer to Abstract Type DataProtocol that receives packets
  DataProtocol* mDataProtocolReceiver;
  AudioInterface* mAudioInterface; ///< Interface to Jack Client
  PacketHeader* mPacketHeader; ///< Pointer to Packet Header
  underrunModeT mUnderRunMode; ///< underrunModeT Mode

  /// Pointer for the Send RingBuffer
  RingBuffer* mSendRingBuffer;
  /// Pointer for the Receive RingBuffer
  RingBuffer* mReceiveRingBuffer;

  int mReceiverBindPort; ///< Incoming (receiving) port for local machine
  int mSenderPeerPort; ///< Incoming (receiving) port for peer machine
  int mSenderBindPort; ///< Outgoing (sending) port for local machine
  int mReceiverPeerPort; ///< Outgoing (sending) port for peer machine
  int mTcpServerPort;

  unsigned int mRedundancy; ///< Redundancy factor in network data
  const char* mJackClientName; ///< JackAudio Client Name

  JackTrip::connectionModeT mConnectionMode; ///< Connection Mode

  QVector<ProcessPlugin*> mProcessPlugins; ///< Vector of ProcesPlugin<EM>s</EM>

  volatile bool mReceivedConnection; ///< Bool of received connection from peer
  volatile bool mTcpConnectionError;
  volatile bool mStopped;
};

#endif