JackTrip.cpp 8.81 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();
jcaceres's avatar
jcaceres committed
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
}


//*******************************************************************************
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;
  mAudioBufferSize = mJackAudio->getBufferSize();
  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
97
98
99
  cout << "The Number of Channels is: " << mJackAudio->getNumInputChannels() << endl;
  std::cout << gPrintSeparator << std::endl;
  usleep(100);
jcaceres's avatar
jcaceres committed
100
101
102
103
104
105
106
107
108
109
110
}


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


//*******************************************************************************
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
169
170
171
  //cout << "Peer Address Set to CACA: " << qPrintable(mPeerAddress) << endl;
  //cout << gPrintSeparator << endl;
  //usleep(300000);
jcaceres's avatar
jcaceres committed
172
173
174
175
176
177
178
  // Set Peer Address in Protocols
}


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


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

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

#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;
211
212
  mDataProtocolSender->start(QThread::TimeCriticalPriority);
  mDataProtocolReceiver->start(QThread::TimeCriticalPriority);
213
214
215
216
#else
  std::cerr << "ERROR: Platform unknown or not supported" << endl;
  std::exit(1);
#endif
217
218
219
220

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


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


jcaceres's avatar
jcaceres committed
239
//*******************************************************************************
jcaceres's avatar
jcaceres committed
240
void JackTrip::serverStart()
jcaceres's avatar
jcaceres committed
241
{
242
243
244
245
246
247
248
  // 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
249
  cout << "Waiting for Connection From Client..." << endl;
250
251
252
253
254
255
256
257
  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
258
  usleep(100);
259

260
261
  // Set the peer address to send packets (in the protocol sender)
  mDataProtocolSender->setPeerAddress( mPeerAddress.toLatin1().data() );
262
}
jcacerec's avatar
jcacerec committed
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




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;
  }
}


void JackTrip::putHeaderInPacket(int8_t* full_packet)
{
  mPacketHeader->fillHeaderCommonFromJack(*mJackAudio);
}