Codebase list jacktrip / upstream/1.3.0+ds0 src / JMess.cpp
upstream/1.3.0+ds0

Tree @upstream/1.3.0+ds0 (Download .tar.gz)

JMess.cpp @upstream/1.3.0+ds0raw · 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
  JMess: A simple utility so save your jack-audio mess.

  Copyright (C) 2007-2010 Juan-Pablo Caceres.

  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.
*/


/*
 * JMess.cpp
 */

#include "JMess.h"
#include "JackTrip.h"
#include "jacktrip_globals.h"
#include <QDebug>

// sJackMutex definition
QMutex JMess::sJMessMutex;

//-------------------------------------------------------------------------------
/*! \brief Constructs a JMess object that has a jack client.
 *
 */
//-------------------------------------------------------------------------------
JMess::JMess()
{
    //Open a client connection to the JACK server.  Starting a
    //new server only to list its ports seems pointless, so we
    //specify JackNoStartServer.
    mClient = jack_client_open ("lsp", JackNoStartServer, &mStatus);
    if (mClient == NULL) {
        if (mStatus & JackServerFailed) {
            cerr << "JACK server not running" << endl;
        } else {
            cerr << "jack_client_open() failed, "
                 << "status = 0x%2.0x\n" << mStatus << endl;
        }
        exit(1);
    }
}


//-------------------------------------------------------------------------------
/*! \brief Distructor closes the jmess jack audio client.
 *
 */
//-------------------------------------------------------------------------------
JMess::~JMess()
{
    if (jack_client_close(mClient))
        cerr << "ERROR: Could not close the hidden jmess jack client." << endl;
}


//-------------------------------------------------------------------------------
/*! \brief Write an XML file with the name specified at xmlOutFile.
 *
 */
//-------------------------------------------------------------------------------
void JMess::writeOutput(__attribute__((unused)) QString xmlOutFile)
{
    //  QDomDocument jmess_xml;   QDomElement root;
    //  QDomElement connection;   QDomElement output;
    //  QDomElement input;        QDomText output_name;
    //  QDomText input_name;

    //  QVector<QString> OutputInput(2);

    //  this->setConnectedPorts();

    //  root = jmess_xml.createElement("jmess");
    //  for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
    //       it != mConnectedPorts.end(); ++it) {
    //    OutputInput = *it;
    //    //cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
    //    //cout << "Input ===> " <<qPrintable(OutputInput[1]) << endl;

    //    //Initialize XML elements
    //    connection = jmess_xml.createElement("connection");
    //    output = jmess_xml.createElement("output");
    //    input = jmess_xml.createElement("input");
    //    output_name = jmess_xml.createTextNode(OutputInput[0]);
    //    input_name = jmess_xml.createTextNode(OutputInput[1]);

    //    jmess_xml.appendChild(root);      root.appendChild(connection);
    //    connection.appendChild(output);   connection.appendChild(input);
    //    output.appendChild(output_name);  input.appendChild(input_name);
    //  }

    //  //Write output file
    //  QFile file(xmlOutFile);
    //  string answer = "";
    //  //Check for existing file first, and confirm before overwriting
    //  if (file.exists()) {
    //    while ((answer != "yes") && (answer != "no")) {
    //      cout << "WARNING: The File " <<qPrintable(xmlOutFile)
    //	   << " exists. Do you want to overwrite it? (yes/no): ";
    //      cin >> answer;
    //    }
    //  }
    //  else {
    //    answer = "yes";
    //  }

    //  if (answer == "yes") {
    //    if (!file.open(QIODevice::WriteOnly)) {
    //      cerr << "Cannot open file for writing: "
    //	   << qPrintable(file.errorString()) << endl;
    //      exit(1);
    //    }

    //    QTextStream out(&file);
    //    jmess_xml.save(out, Indent);
    //    cout << qPrintable(xmlOutFile) << " written." << endl;
    //  }
}


//-------------------------------------------------------------------------------
/*! \brief Set list of ouput ports that have connections.
 *
 */
//-------------------------------------------------------------------------------
void JMess::setConnectedPorts()
{
    mConnectedPorts.clear();

    const char **ports, **connections; //vector of ports and connections
    QVector<QString> OutputInput(2); //helper variable

    //Get active output ports.
    ports = jack_get_ports (mClient, NULL, NULL, JackPortIsOutput);

    for (unsigned int out_i = 0; ports[out_i]; ++out_i) {
        if ((connections = jack_port_get_all_connections
             (mClient, jack_port_by_name(mClient, ports[out_i]))) != 0) {
            for (unsigned int in_i = 0; connections[in_i]; ++in_i) {
                OutputInput[0] = ports[out_i];
                //    cout << "Output ===> " <<qPrintable(OutputInput[0]) << endl;
                OutputInput[1] = connections[in_i];
                //    cout << "Input ===> " << qPrintable(OutputInput[1]) << endl;
                mConnectedPorts.append(OutputInput);
            }
        }
    }

    free(ports);
}
//*******************************************************************************
void JMess::connectSpawnedPorts(int nChans, int hubPatch)
// called from UdpHubListener::connectMesh
{
    QMutexLocker locker(&sJMessMutex);
    
    QString IPS[gMAX_WAIRS];
    int ctr = 0;

    const char **ports; //, **connections; //vector of ports and connections
    QVector<QString> OutputInput(2); //helper variable

    //Get active output ports.
    ports = jack_get_ports (mClient, NULL, NULL, JackPortIsOutput);

    for (unsigned int out_i = 0; ports[out_i]; ++out_i) {
        //        qDebug() << QString(ports[out_i]);
        bool systemPort = QString(ports[out_i]).contains(QString("system"));

        QString str = QString(ports[out_i]);
        //  for example              "171.64.197.121:receive_1"
        QString s = str.section(':', 0, 0);
        //        qDebug() << s << systemPort;
        //  for example              "171.64.197.121"

        bool newOne = !systemPort;
        for (int i = 0; i<ctr; i++) if (newOne && (IPS[i]==s)) newOne = false;
        if (newOne)
        {
            IPS[ctr] = s;
            ctr++;
            //                        qDebug() << ports[out_i] << systemPort << s;
        }
    }
    //for (int i = 0; i<ctr; i++) qDebug() << IPS[i];
    disconnectAll();

    int k = 0;
    int jLimit = 1;

    // FULLMIX is the union of CLIENTFOFI, CLIENTECHO

    // implements CLIENTFOFI, CLIENTECHO -- also FULLMIX part which is CLIENTECHO
    for (int i = 0; i<ctr; i++) {
        if (hubPatch == JackTrip::CLIENTFOFI) jLimit = (ctr-1);
        for (int j = 0; j<jLimit; j++) {
            if ((hubPatch == JackTrip::CLIENTECHO)||(hubPatch == JackTrip::FULLMIX)) k = i;
            else if (hubPatch == JackTrip::CLIENTFOFI) k = (j+(i+1))%ctr;
            for (int l = 1; l<=nChans; l++) { // chans are 1-based
                //qDebug() << "connect " << IPS[i]+":receive_"+QString::number(l)
                         //<<"with " << IPS[k]+":send_"+QString::number(l);

                QString left = IPS[i] +
                        ":receive_" + QString::number(l);
                QString right = IPS[k] +
                        ":send_" + QString::number(l);

                if (0 !=
                        jack_connect(mClient, left.toStdString().c_str(), right.toStdString().c_str())) {
                    qDebug() << "WARNING: port: " << left
                             << "and port: " << right
                             << " could not be connected.";
                }
            }
        }
    }

    // do it again to implement the FULLMIX part which is CLIENTFOFI
    if (hubPatch == JackTrip::FULLMIX) {
        jLimit = (ctr-1); // same as CLIENTFOFI
        /*************/
        // todo: the next block should be in a method, it's a repeat of the above
        for (int i = 0; i<ctr; i++) {
            for (int j = 0; j<jLimit; j++) {
                k = (j+(i+1))%ctr;
                for (int l = 1; l<=nChans; l++) { // chans are 1-based
                    //qDebug() << "connect " << IPS[i]+":receive_"+QString::number(l)
                             //<<"with " << IPS[k]+":send_"+QString::number(l);

                    QString left = IPS[i] +
                            ":receive_" + QString::number(l);
                    QString right = IPS[k] +
                            ":send_" + QString::number(l);

                    if (0 !=
                            jack_connect(mClient, left.toStdString().c_str(), right.toStdString().c_str())) {
                        qDebug() << "WARNING: port: " << left
                                 << "and port: " << right
                                 << " could not be connected.";
                    }
                }
            }
        }
    }

    free(ports);
}

//*******************************************************************************
// connectTUB is called when in hubpatch mode 4 = RESERVEDMATRIX
// TU Berlin Raspberry Pi ensemble, Winter 2019
// this gets run on the ensemble's hub server with
// ./jacktrip -S -p3
// it connects a set of client jacktrips with known hardwired IP addresses
// to a known hardwired audio process with known hardwired audio port names
// when clients connect / disconnect dynamically this just runs through the
// audio connection sequence bruteforce at every new connection change
// those that are preexisting won't change
// a new one will connect accordingly and
// those that fail because they don't exist will fail, no worries

// setting the connections tested with jacktrip_globals.h
// const QString gDOMAIN_TRIPLE = QString("130.149.23"); // for TUB multiclient hub
// const int gMIN_TUB = 215; // lowest client address
// const int gMAX_TUB = 215; // highest client address

///////////////////////////////
// test NUC as server
//#define HARDWIRED_AUDIO_PROCESS_ON_SERVER "par20straightWire"
//#define ENUMERATE ""
//#define HARDWIRED_AUDIO_PROCESS_ON_SERVER_IN ":in_"
//#define HARDWIRED_AUDIO_PROCESS_ON_SERVER_OUT ":out_"

///////////////////////////////
// test Riviera as server
// for deployment change jacktrip_globals.h to
// const QString gDOMAIN_TRIPLE = QString("192.168.0"); // for TUB multiclient hub
// const int gMIN_TUB = 11; // lowest client address
// const int gMAX_TUB = 20; // highest client address
// and give the proper audio process and connection names

#define HARDWIRED_AUDIO_PROCESS_ON_SERVER "SuperCollider"
#define HARDWIRED_AUDIO_PROCESS_ON_SERVER_IN ":in_"
#define HARDWIRED_AUDIO_PROCESS_ON_SERVER_OUT ":out_"
// On server side it is SC jack-clients with indivisual names:
// POE_0...POE_16
// and each has (at this moment) one port in/out:
// receive_1
// send_1
// I think it should be extended to 4 in/out ports per client.

// this is brute force, does not look at individual clients, just patches the whole ensemble
// each time
void JMess::connectTUB(int /*nChans*/)
// called from UdpHubListener::connectPatch
{
    for (int i = 0; i<=gMAX_TUB-gMIN_TUB; i++) // last IP decimal octet
        for (int l = 1; l<=1; l++) // mono for now // chans are 1-based, 1...2
        {
            // jacktrip to SC
            QString client = gDOMAIN_TRIPLE + QString(".") + QString::number(gMIN_TUB+i);
            QString serverAudio = QString(HARDWIRED_AUDIO_PROCESS_ON_SERVER);
            int tmp = i + l; // only works for mono... completely wrong for 2 or more chans
            qDebug() << "connect " << client << ":receive_ " << l
                     <<"with " << serverAudio << HARDWIRED_AUDIO_PROCESS_ON_SERVER_IN << tmp;

            QString left = QString(client + ":receive_" + QString::number(l));
            QString right = QString(serverAudio + HARDWIRED_AUDIO_PROCESS_ON_SERVER_IN +
                                    QString::number(tmp));

            if (0 !=
                    jack_connect(mClient, left.toStdString().c_str(),
                                 right.toStdString().c_str())) {
                qDebug() << "WARNING: port: " << left
                         << "and port: " << right
                         << " could not be connected.";
            }

            // SC to jacktrip
            tmp += 4; // increase tmp for port offest
            qDebug() << "connect " << serverAudio << HARDWIRED_AUDIO_PROCESS_ON_SERVER_OUT
                     << tmp <<"with " << client << ":send_" << l;

            left = QString(serverAudio + HARDWIRED_AUDIO_PROCESS_ON_SERVER_OUT +
                           QString::number(tmp));
            right = QString(client + ":send_" + QString::number(l));

            if (0 !=
                    jack_connect(mClient, left.toStdString().c_str(),
                                 right.toStdString().c_str())) {
                qDebug() << "WARNING: port: " << left
                         << "and port: " << right
                         << " could not be connected.";
            }

        }
}

//-------------------------------------------------------------------------------
/*! \brief Disconnect all the clients.
 *
 */
//-------------------------------------------------------------------------------
void JMess::disconnectAll()
{
    QVector<QString> OutputInput(2);

    this->setConnectedPorts();

    for (QVector<QVector<QString> >::iterator it = mConnectedPorts.begin();
         it != mConnectedPorts.end(); ++it) {
        OutputInput = *it;

        if (jack_disconnect(mClient, OutputInput[0].toUtf8(), OutputInput[1].toUtf8())) {
            cerr << "WARNING: port: " << qPrintable(OutputInput[0])
                    << "and port: " << qPrintable(OutputInput[1])
                    << " could not be disconnected.\n";
        }
    }

}


//-------------------------------------------------------------------------------
/*! \brief Parse the XML input file.
 *
 * Returns 0 on success, or 1 if the file has an incorrect format or cannot
 * read the file.
 */
//-------------------------------------------------------------------------------
int JMess::parseXML(__attribute__((unused)) QString xmlInFile)
{
    //  mPortsToConnect.clear();
    //  QString errorStr;
    //  int errorLine;
    //  int errorColumn;

    //  QFile file(xmlInFile);
    //  if (!file.open(QIODevice::ReadOnly)) {
    //    cerr << "Cannot open file for reading: "
    //	 << qPrintable(file.errorString()) << endl;
    //    return 1;
    //  }

    //  QDomDocument doc;
    //  if (!doc.setContent(&file, true, &errorStr, &errorLine,
    //		      &errorColumn)) {
    //    cerr << "===================================================\n"
    //	 << "Error parsing XML input file:\n"
    //	 << "Parse error at line " << errorLine
    //	 << ", column " << errorColumn << "\n"
    //	 << qPrintable(errorStr) << "\n"
    //	 << "===================================================\n";
    //    return 1;
    //  }

    //  QDomElement jmess = doc.documentElement();
    //  if (jmess.tagName() != "jmess") {
    //    cerr << "Error: Root tag should be <jmess>: "
    //	 << qPrintable(jmess.tagName()) << endl;
    //    return 1;
    //  }


    //  QVector<QString> OutputInput(2);
    //  //First check for <connection> tag
    //  for(QDomNode n_cntn = jmess.firstChild();
    //      !n_cntn.isNull(); n_cntn = n_cntn.nextSibling()) {
    //    QDomElement cntn = n_cntn.toElement();
    //    if (cntn.tagName() == "connection") {
    //      //Now check for ouput & input tag
    //      for(QDomNode n_sck = cntn.firstChild();
    //	  !n_sck.isNull(); n_sck = n_sck.nextSibling()) {
    //	QDomElement sck = n_sck.toElement();
    //	//cout << qPrintable(sck.tagName()) << endl;
    //	//cout << qPrintable(sck.text()) << endl;
    //	if (sck.tagName() == "output") {
    //	  OutputInput[0] = sck.text();
    //	}
    //	else if (sck.tagName() == "input") {
    //	  OutputInput[1] = sck.text();
    //	}
    //      }
    //      mPortsToConnect.append(OutputInput);
    //    }
    //  }

    return 0;

}


//-------------------------------------------------------------------------------
/*! \brief Connect ports specified in input XML file xmlInFile
 *
 */
//-------------------------------------------------------------------------------
void JMess::connectPorts(__attribute__((unused)) QString xmlInFile)
{
    QVector<QString> OutputInput(2);

    //  if ( !(this->parseXML(xmlInFile)) ) {
    //    for (QVector<QVector<QString> >::iterator it = mPortsToConnect.begin();
    //	 it != mPortsToConnect.end(); ++it) {
    //      OutputInput = *it;

    //      if (jack_connect(mClient, OutputInput[0].toLatin1(), OutputInput[1].toLatin1())) {
    //	//Display a warining only if the error is not because the ports are already
    //	//connected, in case the program doesn't display anyting.
    //	if (EEXIST !=
    //        jack_connect(mClient, OutputInput[0].toLatin1(), OutputInput[1].toLatin1())) {
    //	  cerr << "WARNING: port: " << qPrintable(OutputInput[0])
    //	       << "and port: " << qPrintable(OutputInput[1])
    //	       << " could not be connected.\n";
    //	}
    //      }
    //    }
    //  }

}