JackTrip.cpp 9.87 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
//*****************************************************************
/*
jcaceres's avatar
jcaceres committed
3
  JackTrip: A System for High-Quality Audio Network Performance
jcaceres's avatar
jcaceres committed
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
  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.
*/
//*****************************************************************

/**
jcaceres's avatar
jcaceres committed
33
 * \file JackTrip.cpp
jcaceres's avatar
jcaceres committed
34
35
36
37
 * \author Juan-Pablo Caceres
 * \date July 2008
 */

jcaceres's avatar
jcaceres committed
38
#include "JackTrip.h"
jcaceres's avatar
jcaceres committed
39
40
41
42
#include "UdpDataProtocol.h"
#include "globals.h"

#include <iostream>
43
#include <unistd.h> // for usleep, sleep
jcacerec's avatar
jcacerec committed
44
#include <cstdlib>
45
46
47

#include <QHostAddress>
#include <QThread>
jcaceres's avatar
jcaceres committed
48

jcaceres's avatar
jcaceres committed
49
using std::cout; using std::endl;
jcaceres's avatar
jcaceres committed
50

jcaceres's avatar
jcaceres committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


//*******************************************************************************
JackTrip::JackTrip(jacktripModeT JacktripMode,
		   dataProtocolT DataProtocolType,
		   int NumChans,
		   int BufferQueueLength,
		   JackAudioInterface::audioBitResolutionT AudioBitResolution) :
  mJackTripMode(JacktripMode),
  mDataProtocol(DataProtocolType),
  mNumChans(NumChans),
  mBufferQueueLength(BufferQueueLength),
  mSampleRate(0),
  mAudioBufferSize(0),
  mAudioBitResolution(AudioBitResolution),
  mDataProtocolSender(NULL),
  mDataProtocolReceiver(NULL),
  mJackAudio(NULL)
{
jcaceres's avatar
jcaceres committed
70
  setupJackAudio();
71
72
73

  /// \todo CHECK THIS AND PUT IT IN A BETTER PLACE, also, get header type from options
  createHeader(DataProtocol::DEFAULT);
jcaceres's avatar
jcaceres committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}


//*******************************************************************************
JackTrip::~JackTrip()
{
  delete mDataProtocolSender;
  delete mDataProtocolReceiver;
  delete mJackAudio;
}


//*******************************************************************************
void JackTrip::setupJackAudio()
{
  // Create JackAudioInterface Client Object
  mJackAudio = new JackAudioInterface(mNumChans, mNumChans, mAudioBitResolution);
  mSampleRate = mJackAudio->getSampleRate();
  std::cout << "The Sampling Rate is: " << mSampleRate << std::endl;
  std::cout << gPrintSeparator << std::endl;
94
  mAudioBufferSize = mJackAudio->getBufferSizeInSamples();
jcaceres's avatar
jcaceres committed
95
96
97
98
99
  int AudioBufferSizeInBytes = mAudioBufferSize*sizeof(sample_t);
  std::cout << "The Audio Buffer Size is: " << mAudioBufferSize << " samples" << std::endl;
  std::cout << "                      or: " << AudioBufferSizeInBytes 
	    << " bytes" << std::endl;
  std::cout << gPrintSeparator << std::endl;
jcaceres's avatar
jcaceres committed
100
101
102
  cout << "The Number of Channels is: " << mJackAudio->getNumInputChannels() << endl;
  std::cout << gPrintSeparator << std::endl;
  usleep(100);
jcaceres's avatar
jcaceres committed
103
104
105
106
107
108
109
110
111
112
113
}


//*******************************************************************************
void JackTrip::setupDataProtocol()
{
  // Create DataProtocol Objects
  switch (mDataProtocol) {
  case UDP:
    std::cout << "Using UDP Protocol" << std::endl;
    std::cout << gPrintSeparator << std::endl;
jcaceres's avatar
jcaceres committed
114
    usleep(100);
jcacerec's avatar
jcacerec committed
115
116
    mDataProtocolSender = new UdpDataProtocol(this, DataProtocol::SENDER);
    mDataProtocolReceiver =  new UdpDataProtocol(this, DataProtocol::RECEIVER);
jcaceres's avatar
jcaceres committed
117
118
119
    break;
  case TCP:
    std::cerr << "ERROR: TCP Protocol is not unimplemented" << std::endl;
jcacerec's avatar
jcacerec committed
120
    std::exit(1);
jcaceres's avatar
jcaceres committed
121
122
123
    break;
  case SCTP:
    std::cerr << "ERROR: SCTP Protocol is not unimplemented" << std::endl;
jcacerec's avatar
jcacerec committed
124
    std::exit(1);
jcaceres's avatar
jcaceres committed
125
126
127
    break;
  default: 
    std::cerr << "ERROR: Protocol not defined or unimplemented" << std::endl;
jcacerec's avatar
jcacerec committed
128
    std::exit(1);
jcaceres's avatar
jcaceres committed
129
130
131
    break;
  }
  
jcaceres's avatar
jcaceres committed
132
133
134
135
136
  // Set Audio Packet Size
  mDataProtocolSender->setAudioPacketSize
    (mJackAudio->getSizeInBytesPerChannel() * mNumChans);
  mDataProtocolReceiver->setAudioPacketSize
    (mJackAudio->getSizeInBytesPerChannel() * mNumChans);
jcaceres's avatar
jcaceres committed
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
}


//*******************************************************************************
void JackTrip::setupRingBuffers()
{
  // Create RingBuffers with the apprioprate size
  /// \todo Make all this operations cleaner
  mSendRingBuffer.reset( new RingBuffer(mJackAudio->getSizeInBytesPerChannel() * mNumChans,
					gDefaultOutputQueueLength) );
  mReceiveRingBuffer.reset( new RingBuffer(mJackAudio->getSizeInBytesPerChannel() * mNumChans,
					   mBufferQueueLength) );

  // Set RingBuffers pointers in protocols
  if ( (mDataProtocolSender == NULL) ||
       (mDataProtocolReceiver == NULL) || (mJackAudio == NULL) ) {
    std::cerr << "ERROR: DataProtocols or JackAudio have not been inizialized" << std::endl;
    std::exit(1);
  }
  else {
    // Set Ring Buffers
    mDataProtocolSender->setRingBuffer(mSendRingBuffer);
    mDataProtocolReceiver->setRingBuffer(mReceiveRingBuffer);
    mJackAudio->setRingBuffers(mSendRingBuffer, mReceiveRingBuffer);

    // Set the header from jack
    mDataProtocolSender->fillHeaderCommonFromJack(*mJackAudio);
  }
}


//*******************************************************************************
void JackTrip::setPeerAddress(char* PeerHostOrIP)
{
  mPeerAddress = PeerHostOrIP;
jcaceres's avatar
jcaceres committed
172
173
174
  //cout << "Peer Address Set to CACA: " << qPrintable(mPeerAddress) << endl;
  //cout << gPrintSeparator << endl;
  //usleep(300000);
jcaceres's avatar
jcaceres committed
175
176
177
178
179
180
181
  // Set Peer Address in Protocols
}


//*******************************************************************************
void JackTrip::appendProcessPlugin(const std::tr1::shared_ptr<ProcessPlugin> plugin)
{
jcaceres's avatar
jcaceres committed
182
  cout << "BEFORE" << endl;
jcaceres's avatar
jcaceres committed
183
  mJackAudio->appendProcessPlugin(plugin);
jcaceres's avatar
jcaceres committed
184
  cout << "AFTER" << endl;
jcaceres's avatar
jcaceres committed
185
186
187
188
}


//*******************************************************************************
jcaceres's avatar
jcaceres committed
189
190
void JackTrip::start()
{
191
192
193
194
195
  // Set all classes and parameters
  setupDataProtocol();
  setupRingBuffers();

  // Start the threads for the specific mode
jcaceres's avatar
jcaceres committed
196
197
198
199
200
201
202
203
204
  switch ( mJackTripMode )
    {
    case CLIENT :
      clientStart();
      break;
    case SERVER :
      serverStart();
      break;
    }
205
206
  // Start Threads
  mJackAudio->startProcess();
207
208
209
210
211
212
213

#if defined ( __LINUX__ )
  cout << "Using Linux thread priority" << endl;
  mDataProtocolSender->start();
  mDataProtocolReceiver->start();
#elif defined ( __MAC_OSX__ )
  cout << "Using qt4 (MAC OS X) thread priority" << endl;
214
215
  mDataProtocolSender->start(QThread::TimeCriticalPriority);
  mDataProtocolReceiver->start(QThread::TimeCriticalPriority);
216
217
218
219
#else
  std::cerr << "ERROR: Platform unknown or not supported" << endl;
  std::exit(1);
#endif
220
221
222
223

  // Wait here until the threads return from run() methos
  mDataProtocolSender->wait();
  mDataProtocolReceiver->wait();
jcaceres's avatar
jcaceres committed
224
225
226
227
228
}


//*******************************************************************************
void JackTrip::clientStart()
jcaceres's avatar
jcaceres committed
229
{
230
  // For the Client mode, the peer (or server) address has to be specified by the user
jcaceres's avatar
jcaceres committed
231
232
233
234
235
  if ( mPeerAddress.isEmpty() ) {
    std::cerr << "ERROR: Peer Address has to be set if you run in CLIENT mode" << endl;
    std::exit(1);
  }
  else {
236
    // Set the peer address
jcaceres's avatar
jcaceres committed
237
238
    mDataProtocolSender->setPeerAddress( mPeerAddress.toLatin1().data() );
  }
jcaceres's avatar
jcaceres committed
239
240
241
}


jcaceres's avatar
jcaceres committed
242
//*******************************************************************************
jcaceres's avatar
jcaceres committed
243
void JackTrip::serverStart()
jcaceres's avatar
jcaceres committed
244
{
245
246
247
248
249
250
251
  // Set the peer address
  if ( !mPeerAddress.isEmpty() ) {
    std::cout << "WARNING: SERVER mode: Peer Address was set but will be deleted." << endl;
    mPeerAddress.clear();
  }

  // Get the client address when it connects
jcaceres's avatar
jcaceres committed
252
  cout << "Waiting for Connection From Client..." << endl;
253
254
255
256
257
258
259
260
  QHostAddress peerHostAddress;
  uint16_t port;
  mDataProtocolReceiver->getPeerAddressFromFirstPacket(peerHostAddress,
						       port);
  mPeerAddress = peerHostAddress.toString();
  cout << "Client Connection Received from IP : " 
       << qPrintable(mPeerAddress) << endl;
  cout << gPrintSeparator << endl;
jcaceres's avatar
jcaceres committed
261
  usleep(100);
262

263
264
  // Set the peer address to send packets (in the protocol sender)
  mDataProtocolSender->setPeerAddress( mPeerAddress.toLatin1().data() );
265
}
jcacerec's avatar
jcacerec committed
266
267


268
//*******************************************************************************
jcacerec's avatar
jcacerec committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
void JackTrip::createHeader(const DataProtocol::packetHeaderTypeT headertype)
{
  switch (headertype) {
  case DataProtocol::DEFAULT :
    mPacketHeader = new DefaultHeader;
    break;
  case DataProtocol::JAMLINK :
    //mHeader = new JamLinkHeader;
    break;
  default :
    std::cerr << "ERROR: Undefined Header Type" << endl;
    std::cerr << "Exiting Program..." << endl;
    std::exit(1);
    break;
  }
}


287
288
//*******************************************************************************
void JackTrip::putHeaderInPacket(int8_t* full_packet, int8_t* audio_packet)
jcacerec's avatar
jcacerec committed
289
290
{
  mPacketHeader->fillHeaderCommonFromJack(*mJackAudio);
291
292
293
294
295
296
297
298
299
300
301
302
303
  mPacketHeader->putHeaderInPacket(full_packet);
  
  int8_t* audio_part;
  audio_part = full_packet + mPacketHeader->getHeaderSizeInBytes();
  std::memcpy(audio_part, audio_packet, mJackAudio->getBufferSizeInBytes());

}


//*******************************************************************************
int JackTrip::getPacketSizeInBytes() const
{
  return (mJackAudio->getBufferSizeInBytes() + mPacketHeader->getHeaderSizeInBytes());
jcacerec's avatar
jcacerec committed
304
305
}

306
307
308
309
310
311
312
313

//*******************************************************************************
void JackTrip::parseAudioPacket(int8_t* full_packet, int8_t* audio_packet)
{
  int8_t* audio_part;
  audio_part = full_packet + mPacketHeader->getHeaderSizeInBytes();
  std::memcpy(audio_packet, audio_part, mJackAudio->getBufferSizeInBytes());
}