JackNetManager.cpp 33.4 KB
Newer Older
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2008-2011 Romain Moret at Grame
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "JackNetManager.h"
20
#include "JackArgParser.h"
sletz's avatar
sletz committed
21
#include "JackTime.h"
22
23
24

using namespace std;

25

26
27
28
29
namespace Jack
{
//JackNetMaster******************************************************************************************************

30
    JackNetMaster::JackNetMaster ( JackNetSocket& socket, session_params_t& params, const char* multicast_ip)
moret's avatar
moret committed
31
            : JackNetMasterInterface ( params, socket, multicast_ip )
32
33
    {
        jack_log ( "JackNetMaster::JackNetMaster" );
moret's avatar
moret committed
34

35
36
37
        //settings
        fClientName = const_cast<char*> ( fParams.fName );
        fJackClient = NULL;
38
39
40
        fSendTransportData.fState = -1;
        fReturnTransportData.fState = -1;
        fLastTransportState = -1;
sletz's avatar
sletz committed
41
        int port_index;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

        //jack audio ports
        fAudioCapturePorts = new jack_port_t* [fParams.fSendAudioChannels];
        for ( port_index = 0; port_index < fParams.fSendAudioChannels; port_index++ )
            fAudioCapturePorts[port_index] = NULL;
        fAudioPlaybackPorts = new jack_port_t* [fParams.fReturnAudioChannels];
        for ( port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++ )
            fAudioPlaybackPorts[port_index] = NULL;
        //jack midi ports
        fMidiCapturePorts = new jack_port_t* [fParams.fSendMidiChannels];
        for ( port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
            fMidiCapturePorts[port_index] = NULL;
        fMidiPlaybackPorts = new jack_port_t* [fParams.fReturnMidiChannels];
        for ( port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
            fMidiPlaybackPorts[port_index] = NULL;

moret's avatar
moret committed
58
        //monitor
59
#ifdef JACK_MONITOR
moret's avatar
moret committed
60
        fPeriodUsecs = ( int ) ( 1000000.f * ( ( float ) fParams.fPeriodSize / ( float ) fParams.fSampleRate ) );
moret's avatar
moret committed
61
62
        string plot_name;
        plot_name = string ( fParams.fName );
moret's avatar
moret committed
63
        plot_name += string ( "_master" );
moret's avatar
moret committed
64
        plot_name += string ( ( fParams.fSlaveSyncMode ) ? "_sync" : "_async" );
moret's avatar
moret committed
65
66
        switch ( fParams.fNetworkMode )
        {
moret's avatar
moret committed
67
68
69
70
71
72
73
74
75
            case 's' :
                plot_name += string ( "_slow" );
                break;
            case 'n' :
                plot_name += string ( "_normal" );
                break;
            case 'f' :
                plot_name += string ( "_fast" );
                break;
moret's avatar
moret committed
76
        }
moret's avatar
moret committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        fNetTimeMon = new JackGnuPlotMonitor<float> ( 128, 4, plot_name );
        string net_time_mon_fields[] =
        {
            string ( "sync send" ),
            string ( "end of send" ),
            string ( "sync recv" ),
            string ( "end of cycle" )
        };
        string net_time_mon_options[] =
        {
            string ( "set xlabel \"audio cycles\"" ),
            string ( "set ylabel \"% of audio cycle\"" )
        };
        fNetTimeMon->SetPlotFile ( net_time_mon_options, 2, net_time_mon_fields, 4 );
moret's avatar
moret committed
91
#endif
92
93
94
95
    }

    JackNetMaster::~JackNetMaster()
    {
sletz's avatar
sletz committed
96
        jack_log ( "JackNetMaster::~JackNetMaster, ID %u", fParams.fID );
moret's avatar
moret committed
97

98
99
100
101
102
103
104
105
106
107
        if ( fJackClient )
        {
            jack_deactivate ( fJackClient );
            FreePorts();
            jack_client_close ( fJackClient );
        }
        delete[] fAudioCapturePorts;
        delete[] fAudioPlaybackPorts;
        delete[] fMidiCapturePorts;
        delete[] fMidiPlaybackPorts;
108
#ifdef JACK_MONITOR
moret's avatar
moret committed
109
110
        fNetTimeMon->Save();
        delete fNetTimeMon;
moret's avatar
moret committed
111
#endif
112
    }
113
//init--------------------------------------------------------------------------------
114
    bool JackNetMaster::Init(bool auto_connect)
115
    {
116
        //network init
sletz's avatar
sletz committed
117
        if (!JackNetMasterInterface::Init()) {
sletz's avatar
sletz committed
118
            jack_error("JackNetMasterInterface::Init() error..." );
119
            return false;
sletz's avatar
sletz committed
120
        }
121

moret's avatar
moret committed
122
        //set global parameters
sletz's avatar
sletz committed
123
124
        if (!SetParams()) {
            jack_error("SetParams error..." );
sletz's avatar
sletz committed
125
            return false;
sletz's avatar
sletz committed
126
        }
moret's avatar
moret committed
127

128
129
130
        //jack client and process
        jack_status_t status;
        if ( ( fJackClient = jack_client_open ( fClientName, JackNullOption, &status, NULL ) ) == NULL )
131
        {
sletz's avatar
sletz committed
132
            jack_error ( "Can't open a new jack client" );
133
134
135
            return false;
        }

136
137
        if (jack_set_process_callback(fJackClient, SetProcess, this ) < 0)
             goto fail;
sletz's avatar
sletz committed
138

139
140
        if (jack_set_buffer_size_callback(fJackClient, SetBufferSize, this) < 0)
             goto fail;
141

142
        if ( AllocPorts() != 0 )
143
        {
sletz's avatar
sletz committed
144
            jack_error ( "Can't allocate jack ports" );
145
            goto fail;
146
147
        }

moret's avatar
moret committed
148
        //process can now run
149
        fRunning = true;
150

151
152
        //finally activate jack client
        if ( jack_activate ( fJackClient ) != 0 )
153
        {
sletz's avatar
sletz committed
154
            jack_error ( "Can't activate jack client" );
155
            goto fail;
156
        }
sletz's avatar
sletz committed
157

158
159
        if (auto_connect)
            ConnectPorts();
sletz's avatar
sletz committed
160
        jack_info ( "New NetMaster started" );
161
        return true;
162

moret's avatar
moret committed
163
    fail:
164
165
166
167
168
169
        FreePorts();
        jack_client_close ( fJackClient );
        fJackClient = NULL;
        return false;
    }

170
//jack ports--------------------------------------------------------------------------
171
172
    int JackNetMaster::AllocPorts()
    {
sletz's avatar
sletz committed
173
        int i;
174
        char name[24];
175
        jack_nframes_t port_latency = jack_get_buffer_size ( fJackClient );
sletz's avatar
sletz committed
176
177
        jack_latency_range_t range;

178
179
        jack_log ( "JackNetMaster::AllocPorts" );

180
181
182
183
        //audio
        for ( i = 0; i < fParams.fSendAudioChannels; i++ )
        {
            sprintf ( name, "to_slave_%d", i+1 );
184
            if ( ( fAudioCapturePorts[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsTerminal, 0 ) ) == NULL )
185
                return -1;
moret's avatar
moret committed
186
            //port latency
sletz's avatar
sletz committed
187
188
            range.min = range.max = 0;
            jack_port_set_latency_range(fAudioCapturePorts[i], JackCaptureLatency, &range);
189
        }
sletz's avatar
sletz committed
190

191
192
193
        for ( i = 0; i < fParams.fReturnAudioChannels; i++ )
        {
            sprintf ( name, "from_slave_%d", i+1 );
194
            if ( ( fAudioPlaybackPorts[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0 ) ) == NULL )
195
                return -1;
moret's avatar
moret committed
196
197
198
199
            //port latency
            switch ( fParams.fNetworkMode )
            {
                case 'f' :
sletz's avatar
sletz committed
200
201
                    range.min = range.max = (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fAudioPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
202
203
                    break;
                case 'n' :
sletz's avatar
sletz committed
204
205
                    range.min = range.max = port_latency + (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fAudioPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
206
207
                    break;
                case 's' :
sletz's avatar
sletz committed
208
209
                    range.min = range.max = 2 * port_latency + (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fAudioPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
210
211
                    break;
            }
212
        }
sletz's avatar
sletz committed
213

214
215
216
217
        //midi
        for ( i = 0; i < fParams.fSendMidiChannels; i++ )
        {
            sprintf ( name, "midi_to_slave_%d", i+1 );
218
            if ( ( fMidiCapturePorts[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput | JackPortIsTerminal, 0 ) ) == NULL )
219
                return -1;
moret's avatar
moret committed
220
            //port latency
sletz's avatar
sletz committed
221
222
            range.min = range.max = 0;
            jack_port_set_latency_range(fMidiCapturePorts[i], JackCaptureLatency, &range);
223
224
225
226
        }
        for ( i = 0; i < fParams.fReturnMidiChannels; i++ )
        {
            sprintf ( name, "midi_from_slave_%d", i+1 );
227
            if ( ( fMidiPlaybackPorts[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_MIDI_TYPE,  JackPortIsOutput | JackPortIsTerminal, 0 ) ) == NULL )
228
                return -1;
moret's avatar
moret committed
229
230
231
232
            //port latency
            switch ( fParams.fNetworkMode )
            {
                case 'f' :
sletz's avatar
sletz committed
233
234
                    range.min = range.max = (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fMidiPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
235
236
                    break;
                case 'n' :
sletz's avatar
sletz committed
237
238
                    range.min = range.max = port_latency + (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fMidiPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
239
240
                    break;
                case 's' :
sletz's avatar
sletz committed
241
242
                    range.min = range.max = 2 * port_latency + (fParams.fSlaveSyncMode) ? 0 : port_latency;
                    jack_port_set_latency_range(fMidiPlaybackPorts[i], JackPlaybackLatency, &range);
moret's avatar
moret committed
243
244
                    break;
            }
245
        }
246
        return 0;
247
    }
sletz's avatar
sletz committed
248

249
250
251
    void JackNetMaster::ConnectPorts()
    {
        const char **ports;
sletz's avatar
sletz committed
252

253
254
        ports = jack_get_ports(fJackClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
        if (ports != NULL) {
sletz's avatar
sletz committed
255
            for (int i = 0; i < fParams.fSendAudioChannels && ports[i]; i++) {
256
257
258
259
                jack_connect(fJackClient, ports[i], jack_port_name(fAudioCapturePorts[i]));
            }
            free(ports);
        }
sletz's avatar
sletz committed
260

261
262
        ports = jack_get_ports(fJackClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
        if (ports != NULL) {
sletz's avatar
sletz committed
263
            for (int i = 0; i < fParams.fReturnAudioChannels && ports[i]; i++) {
264
265
266
267
268
                jack_connect(fJackClient, jack_port_name(fAudioPlaybackPorts[i]), ports[i]);
            }
            free(ports);
        }
    }
269
270
271
272

    void JackNetMaster::FreePorts()
    {
        jack_log ( "JackNetMaster::FreePorts, ID %u", fParams.fID );
moret's avatar
moret committed
273

sletz's avatar
sletz committed
274
        int port_index;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        for ( port_index = 0; port_index < fParams.fSendAudioChannels; port_index++ )
            if ( fAudioCapturePorts[port_index] )
                jack_port_unregister ( fJackClient, fAudioCapturePorts[port_index] );
        for ( port_index = 0; port_index < fParams.fReturnAudioChannels; port_index++ )
            if ( fAudioPlaybackPorts[port_index] )
                jack_port_unregister ( fJackClient, fAudioPlaybackPorts[port_index] );
        for ( port_index = 0; port_index < fParams.fSendMidiChannels; port_index++ )
            if ( fMidiCapturePorts[port_index] )
                jack_port_unregister ( fJackClient, fMidiCapturePorts[port_index] );
        for ( port_index = 0; port_index < fParams.fReturnMidiChannels; port_index++ )
            if ( fMidiPlaybackPorts[port_index] )
                jack_port_unregister ( fJackClient, fMidiPlaybackPorts[port_index] );
    }

289
//transport---------------------------------------------------------------------------
290
    void JackNetMaster::EncodeTransportData()
291
292
293
    {
        //is there a new timebase master ?
        //TODO : check if any timebase callback has been called (and if it's conditional or not) and set correct value...
294
        fSendTransportData.fTimebaseMaster = NO_CHANGE;
295
296

        //update state and position
297
        fSendTransportData.fState = static_cast<uint> ( jack_transport_query ( fJackClient, &fSendTransportData.fPosition ) );
298
299

        //is it a new state ?
300
        fSendTransportData.fNewState = ( ( fSendTransportData.fState != fLastTransportState ) &&
moret's avatar
moret committed
301
                                         ( fSendTransportData.fState != fReturnTransportData.fState ) );
302
        if ( fSendTransportData.fNewState )
sletz's avatar
sletz committed
303
            jack_info ( "Sending '%s' to '%s' frame = %ld", GetTransportState ( fSendTransportData.fState ), fParams.fName, fSendTransportData.fPosition.frame );
304
        fLastTransportState = fSendTransportData.fState;
305
   }
306

307
    void JackNetMaster::DecodeTransportData()
308
    {
309
        //is there timebase master change ?
310
        if ( fReturnTransportData.fTimebaseMaster != NO_CHANGE )
311
        {
312
            int timebase = 0;
313
            switch ( fReturnTransportData.fTimebaseMaster )
314
315
316
            {
                case RELEASE_TIMEBASEMASTER :
                    timebase = jack_release_timebase ( fJackClient );
sletz's avatar
sletz committed
317
318
319
320
321
                    if (timebase < 0) {
                        jack_error("Can't release timebase master");
                    } else {
                        jack_info("'%s' isn't the timebase master anymore", fParams.fName);
                    }
322
                    break;
sletz's avatar
sletz committed
323

324
325
                case TIMEBASEMASTER :
                    timebase = jack_set_timebase_callback ( fJackClient, 0, SetTimebaseCallback, this );
sletz's avatar
sletz committed
326
327
328
329
330
                    if (timebase < 0) {
                        jack_error("Can't set a new timebase master");
                    } else {
                        jack_info("'%s' is the new timebase master", fParams.fName);
                    }
331
                    break;
sletz's avatar
sletz committed
332

333
334
                case CONDITIONAL_TIMEBASEMASTER :
                    timebase = jack_set_timebase_callback ( fJackClient, 1, SetTimebaseCallback, this );
sletz's avatar
sletz committed
335
336
337
                    if (timebase != EBUSY) {
                        if (timebase < 0)
                            jack_error("Can't set a new timebase master");
338
                        else
sletz's avatar
sletz committed
339
                            jack_info("'%s' is the new timebase master", fParams.fName);
340
341
342
                    }
                    break;
            }
343
344
        }

345
        //is the slave in a new transport state and is this state different from master's ?
moret's avatar
moret committed
346
        if ( fReturnTransportData.fNewState && ( fReturnTransportData.fState != jack_transport_query ( fJackClient, NULL ) ) )
347
        {
348
            switch ( fReturnTransportData.fState )
349
350
351
            {
                case JackTransportStopped :
                    jack_transport_stop ( fJackClient );
sletz's avatar
sletz committed
352
                    jack_info ( "'%s' stops transport", fParams.fName );
353
                    break;
sletz's avatar
sletz committed
354

355
                case JackTransportStarting :
356
                    if ( jack_transport_reposition ( fJackClient, &fReturnTransportData.fPosition ) == EINVAL )
sletz's avatar
sletz committed
357
                        jack_error ( "Can't set new position" );
358
                    jack_transport_start ( fJackClient );
sletz's avatar
sletz committed
359
                    jack_info ( "'%s' starts transport frame = %d", fParams.fName, fReturnTransportData.fPosition.frame);
360
                    break;
sletz's avatar
sletz committed
361

362
                case JackTransportNetStarting :
sletz's avatar
sletz committed
363
                    jack_info ( "'%s' is ready to roll...", fParams.fName );
moret's avatar
moret committed
364
                    break;
sletz's avatar
sletz committed
365

366
                case JackTransportRolling :
sletz's avatar
sletz committed
367
                    jack_info ( "'%s' is rolling", fParams.fName );
368
369
370
371
372
373
374
375
376
377
378
379
                    break;
            }
        }
    }

    void JackNetMaster::SetTimebaseCallback ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t* pos, int new_pos, void* arg )
    {
        static_cast<JackNetMaster*> ( arg )->TimebaseCallback ( pos );
    }

    void JackNetMaster::TimebaseCallback ( jack_position_t* pos )
    {
380
381
382
383
384
385
386
387
        pos->bar = fReturnTransportData.fPosition.bar;
        pos->beat = fReturnTransportData.fPosition.beat;
        pos->tick = fReturnTransportData.fPosition.tick;
        pos->bar_start_tick = fReturnTransportData.fPosition.bar_start_tick;
        pos->beats_per_bar = fReturnTransportData.fPosition.beats_per_bar;
        pos->beat_type = fReturnTransportData.fPosition.beat_type;
        pos->ticks_per_beat = fReturnTransportData.fPosition.ticks_per_beat;
        pos->beats_per_minute = fReturnTransportData.fPosition.beats_per_minute;
388
389
390
    }

//sync--------------------------------------------------------------------------------
sletz's avatar
sletz committed
391

392
393
    bool JackNetMaster::IsSlaveReadyToRoll()
    {
394
        return ( fReturnTransportData.fState == JackTransportNetStarting );
395
    }
sletz's avatar
sletz committed
396

sletz's avatar
sletz committed
397
    int JackNetMaster::SetBufferSize(jack_nframes_t nframes, void* arg)
398
    {
sletz's avatar
sletz committed
399
400
        JackNetMaster* obj = static_cast<JackNetMaster*>(arg);
        if (nframes != obj->fParams.fPeriodSize) {
401
            jack_error("Cannot handle buffer size change, so JackNetMaster proxy will be removed...");
sletz's avatar
sletz committed
402
403
            obj->Exit();
        }
404
405
        return 0;
    }
406
407

//process-----------------------------------------------------------------------------
sletz's avatar
sletz committed
408
    int JackNetMaster::SetProcess(jack_nframes_t nframes, void* arg)
409
    {
410
        return static_cast<JackNetMaster*> ( arg )->Process();
411
412
413
414
    }

    int JackNetMaster::Process()
    {
415
416
        int res;

sletz's avatar
sletz committed
417
        if (!fRunning)
418
419
            return 0;

420
#ifdef JACK_MONITOR
421
        jack_time_t begin_time = GetMicroSeconds();
moret's avatar
Cleanup    
moret committed
422
        fNetTimeMon->New();
moret's avatar
moret committed
423
424
#endif

425
        //buffers
426
427
428
        for (int midi_port_index = 0; midi_port_index < fParams.fSendMidiChannels; midi_port_index++) {
            fNetMidiCaptureBuffer->SetBuffer(midi_port_index,
                                            static_cast<JackMidiBuffer*>(jack_port_get_buffer(fMidiCapturePorts[midi_port_index],
sletz's avatar
sletz committed
429
430
                                            fParams.fPeriodSize)));
        }
431
        for (int audio_port_index = 0; audio_port_index < fParams.fSendAudioChannels; audio_port_index++) {
432
433

        #ifdef OPTIMIZED_PROTOCOL
434
435
436
            if ((long)fNetAudioCaptureBuffer->GetBuffer(audio_port_index) == -1) {
                fNetAudioCaptureBuffer->SetBuffer(audio_port_index,
                                                static_cast<sample_t*>(jack_port_get_buffer_nulled(fAudioCapturePorts[audio_port_index],
437
                                                fParams.fPeriodSize)));
438
439
440
            } else {
                fNetAudioCaptureBuffer->SetBuffer(audio_port_index, NULL);
            }
441
        #else
442
443
            fNetAudioCaptureBuffer->SetBuffer(audio_port_index,
                                                static_cast<sample_t*>(jack_port_get_buffer(fAudioCapturePorts[audio_port_index],
sletz's avatar
sletz committed
444
                                                fParams.fPeriodSize)));
445
        #endif
446
            // TODO
sletz's avatar
sletz committed
447
        }
448
449
450
451

        for (int midi_port_index = 0; midi_port_index < fParams.fReturnMidiChannels; midi_port_index++) {
            fNetMidiPlaybackBuffer->SetBuffer(midi_port_index,
                                                static_cast<JackMidiBuffer*>(jack_port_get_buffer(fMidiPlaybackPorts[midi_port_index],
sletz's avatar
sletz committed
452
453
                                                fParams.fPeriodSize)));
        }
454
        for (int audio_port_index = 0; audio_port_index < fParams.fReturnAudioChannels; audio_port_index++) {
455
456

        #ifdef OPTIMIZED_PROTOCOL
457
458
            fNetAudioPlaybackBuffer->SetBuffer(audio_port_index,
                                                static_cast<sample_t*>(jack_port_get_buffer_nulled(fAudioPlaybackPorts[audio_port_index],
459
460
                                                fParams.fPeriodSize)));
        #else
461
462
            fNetAudioPlaybackBuffer->SetBuffer(audio_port_index,
                                                static_cast<sample_t*>(jack_port_get_buffer(fAudioPlaybackPorts[audio_port_index],
sletz's avatar
sletz committed
463
                                                fParams.fPeriodSize)));
464
        #endif
sletz's avatar
sletz committed
465
        }
466

467
        if (IsSynched()) {  // only send if connection is "synched"
sletz's avatar
sletz committed
468

469
            //encode the first packet
470
            EncodeSyncPacket();
sletz's avatar
sletz committed
471

472
            if (SyncSend() == SOCKET_ERROR)
sletz's avatar
sletz committed
473
                return SOCKET_ERROR;
474
475

    #ifdef JACK_MONITOR
sletz's avatar
sletz committed
476
            fNetTimeMon->Add((((float) (GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs ) * 100.f);
477
478
479
    #endif

            //send data
480
            if (DataSend() == SOCKET_ERROR)
sletz's avatar
sletz committed
481
                return SOCKET_ERROR;
482

483
    #ifdef JACK_MONITOR
sletz's avatar
sletz committed
484
            fNetTimeMon->Add((((float) (GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
485
    #endif
sletz's avatar
sletz committed
486

487
        } else {
sletz's avatar
sletz committed
488
            jack_error("Connection is not synched, skip cycle...");
489
        }
moret's avatar
moret committed
490

491
        //receive sync
492
493
494
495
496
        res = SyncRecv();
        if ((res == 0) || (res == SOCKET_ERROR))
            return res;

        /*
sletz's avatar
sletz committed
497
        switch (SyncRecv()) {
sletz's avatar
sletz committed
498
499
500

            case 0:
                jack_error("Connection is not yet synched, skip cycle...");
sletz's avatar
sletz committed
501
                return 0;
sletz's avatar
sletz committed
502
503
504
505
506
507
508
509
510
511
512
513

            case SOCKET_ERROR:
                jack_error("Connection is lost, quit master...");
                //ask to the manager to properly remove the master
                Exit();
                //UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
                ThreadExit();
                break;

            default:
                break;
        }
514
        */
moret's avatar
moret committed
515

516
#ifdef JACK_MONITOR
sletz's avatar
sletz committed
517
        fNetTimeMon->Add ((((float) (GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
moret's avatar
moret committed
518
#endif
519

520
        //decode sync
521
        DecodeSyncPacket();
sletz's avatar
sletz committed
522

523
        //receive data
524
525
526
527
528
        res = DataRecv();
        if ((res == 0) || (res == SOCKET_ERROR))
            return res;

        /*
sletz's avatar
sletz committed
529
        switch (DataRecv()) {
sletz's avatar
sletz committed
530
531
532

            case 0:
                jack_error("Connection is not yet synched, skip cycle...");
sletz's avatar
sletz committed
533
                return 0;
sletz's avatar
sletz committed
534
535
536
537
538
539
540
541
542
543
544
545

            case SOCKET_ERROR:
                jack_error("Connection is lost, quit master...");
                //ask to the manager to properly remove the master
                Exit();
                //UGLY temporary way to be sure the thread does not call code possibly causing a deadlock in JackEngine.
                ThreadExit();
                break;

            default:
                break;
        }
546
        */
moret's avatar
moret committed
547

548
#ifdef JACK_MONITOR
sletz's avatar
sletz committed
549
        fNetTimeMon->AddLast((((float) (GetMicroSeconds() - begin_time)) / (float) fPeriodUsecs) * 100.f);
moret's avatar
moret committed
550
#endif
551
552
553
554
555
556
557
558
        return 0;
    }

//JackNetMasterManager***********************************************************************************************

    JackNetMasterManager::JackNetMasterManager ( jack_client_t* client, const JSList* params ) : fSocket()
    {
        jack_log ( "JackNetMasterManager::JackNetMasterManager" );
moret's avatar
moret committed
559

560
561
562
563
        fManagerClient = client;
        fManagerName = jack_get_client_name ( fManagerClient );
        fGlobalID = 0;
        fRunning = true;
564
        fAutoConnect = false;
565
566
567

        const JSList* node;
        const jack_driver_param_t* param;
568
569
570
571
572
573
574
575
576
577
578
579

        // Possibly use env variable
        const char* default_udp_port = getenv("JACK_NETJACK_PORT");
        fSocket.SetPort((default_udp_port) ? atoi(default_udp_port) : DEFAULT_PORT);

        const char* default_multicast_ip = getenv("JACK_NETJACK_MULTICAST");
        if (default_multicast_ip) {
            strcpy(fMulticastIP, default_multicast_ip);
        } else {
            strcpy(fMulticastIP, DEFAULT_MULTICAST_IP);
        }

580
581
582
583
584
        for ( node = params; node; node = jack_slist_next ( node ) )
        {
            param = ( const jack_driver_param_t* ) node->data;
            switch ( param->character )
            {
moret's avatar
moret committed
585
                case 'a' :
586
587
588
589
                    if (strlen (param->value.str) < 32)
                        strcpy(fMulticastIP, param->value.str);
                    else
                        jack_error("Can't use multicast address %s, using default %s", param->value.ui, DEFAULT_MULTICAST_IP);
moret's avatar
moret committed
590
                    break;
sletz's avatar
sletz committed
591

moret's avatar
moret committed
592
593
                case 'p':
                    fSocket.SetPort ( param->value.ui );
594
                    break;
sletz's avatar
sletz committed
595

596
597
598
                case 'c':
                    fAutoConnect = param->value.i;
                    break;
599
600
601
            }
        }

602
603
        //set sync callback
        jack_set_sync_callback ( fManagerClient, SetSyncCallback, this );
604

605
        //activate the client (for sync callback)
606
        if ( jack_activate ( fManagerClient ) != 0 )
sletz's avatar
sletz committed
607
            jack_error ( "Can't activate the network manager client, transport disabled" );
608

609
610
        //launch the manager thread
        if ( jack_client_create_thread ( fManagerClient, &fManagerThread, 0, 0, NetManagerThread, this ) )
sletz's avatar
sletz committed
611
            jack_error ( "Can't create the network manager control thread" );
612
613
614
615
616
    }

    JackNetMasterManager::~JackNetMasterManager()
    {
        jack_log ( "JackNetMasterManager::~JackNetMasterManager" );
617
618
        jack_info ( "Exiting net manager..." );
        fRunning = false;
sletz's avatar
sletz committed
619
        jack_client_kill_thread ( fManagerClient, fManagerThread );
620
621
622
623
624
625
626
        master_list_t::iterator it;
        for ( it = fMasterList.begin(); it != fMasterList.end(); it++ )
            delete ( *it );
        fSocket.Close();
        SocketAPIEnd();
    }

sletz's avatar
sletz committed
627
    int JackNetMasterManager::CountIO(int flags)
628
629
630
    {
        const char **ports;
        int count = 0;
sletz's avatar
sletz committed
631
        jack_port_t* port;
632

sletz's avatar
sletz committed
633
        ports = jack_get_ports(fManagerClient, NULL, NULL, flags);
634
        if (ports != NULL) {
sletz's avatar
sletz committed
635
636
637
638
639
            while (ports[count]
                    && (port = jack_port_by_name(fManagerClient, ports[count]))
                    && (strcmp(jack_port_type(port), JACK_DEFAULT_AUDIO_TYPE) == 0)) {
                count++;
            }
640
641
642
643
644
            free(ports);
        }
        return count;
    }

645
646
    int JackNetMasterManager::SetSyncCallback ( jack_transport_state_t state, jack_position_t* pos, void* arg )
    {
647
        return static_cast<JackNetMasterManager*> ( arg )->SyncCallback ( state, pos );
648
649
650
651
    }

    int JackNetMasterManager::SyncCallback ( jack_transport_state_t state, jack_position_t* pos )
    {
652
        //check if each slave is ready to roll
653
654
655
        int ret = 1;
        master_list_it_t it;
        for ( it = fMasterList.begin(); it != fMasterList.end(); it++ )
656
            if ( ! ( *it )->IsSlaveReadyToRoll() )
657
658
                ret = 0;
        jack_log ( "JackNetMasterManager::SyncCallback returns '%s'", ( ret ) ? "true" : "false" );
moret's avatar
moret committed
659
        return ret;
660
661
    }

662
663
664
    void* JackNetMasterManager::NetManagerThread ( void* arg )
    {
        JackNetMasterManager* master_manager = static_cast<JackNetMasterManager*> ( arg );
sletz's avatar
sletz committed
665
        jack_info ( "Starting Jack Network Manager" );
666
        jack_info ( "Listening on '%s:%d'", master_manager->fMulticastIP, master_manager->fSocket.GetPort() );
667
668
669
670
671
672
673
674
675
676
677
        master_manager->Run();
        return NULL;
    }

    void JackNetMasterManager::Run()
    {
        jack_log ( "JackNetMasterManager::Run" );
        //utility variables
        int attempt = 0;

        //data
678
        session_params_t host_params;
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
        int rx_bytes = 0;
        JackNetMaster* net_master;

        //init socket API (win32)
        if ( SocketAPIInit() < 0 )
        {
            jack_error ( "Can't init Socket API, exiting..." );
            return;
        }

        //socket
        if ( fSocket.NewSocket() == SOCKET_ERROR )
        {
            jack_error ( "Can't create the network management input socket : %s", StrError ( NET_ERROR_CODE ) );
            return;
        }

        //bind the socket to the local port
moret's avatar
moret committed
697
        if ( fSocket.Bind() == SOCKET_ERROR )
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
        {
            jack_error ( "Can't bind the network manager socket : %s", StrError ( NET_ERROR_CODE ) );
            fSocket.Close();
            return;
        }

        //join multicast group
        if ( fSocket.JoinMCastGroup ( fMulticastIP ) == SOCKET_ERROR )
            jack_error ( "Can't join multicast group : %s", StrError ( NET_ERROR_CODE ) );

        //local loop
        if ( fSocket.SetLocalLoop() == SOCKET_ERROR )
            jack_error ( "Can't set local loop : %s", StrError ( NET_ERROR_CODE ) );

        //set a timeout on the multicast receive (the thread can now be cancelled)
713
        if ( fSocket.SetTimeOut ( 2000000 ) == SOCKET_ERROR )
714
715
716
717
718
            jack_error ( "Can't set timeout : %s", StrError ( NET_ERROR_CODE ) );

        //main loop, wait for data, deal with it and wait again
        do
        {
719
720
721
            session_params_t net_params;
            rx_bytes = fSocket.CatchHost ( &net_params, sizeof ( session_params_t ), 0 );
            SessionParamsNToH(&net_params, &host_params);
722
723
724
725
726
            if ( ( rx_bytes == SOCKET_ERROR ) && ( fSocket.GetError() != NET_NO_DATA ) )
            {
                jack_error ( "Error in receive : %s", StrError ( NET_ERROR_CODE ) );
                if ( ++attempt == 10 )
                {
sletz's avatar
sletz committed
727
                    jack_error ( "Can't receive on the socket, exiting net manager" );
728
729
730
731
732
                    return;
                }
            }
            if ( rx_bytes == sizeof ( session_params_t ) )
            {
733
                switch ( GetPacketType ( &host_params ) )
734
                {
moret's avatar
moret committed
735
                    case SLAVE_AVAILABLE:
736
                        if ( ( net_master = InitMaster ( host_params ) ) )
moret's avatar
moret committed
737
738
739
                            SessionParamsDisplay ( &net_master->fParams );
                        else
                            jack_error ( "Can't init new net master..." );
moret's avatar
moret committed
740
                        jack_info ( "Waiting for a slave..." );
moret's avatar
moret committed
741
742
                        break;
                    case KILL_MASTER:
743
                        if ( KillMaster ( &host_params ) )
moret's avatar
moret committed
744
745
746
747
                            jack_info ( "Waiting for a slave..." );
                        break;
                    default:
                        break;
748
749
750
751
752
753
                }
            }
        }
        while ( fRunning );
    }

sletz's avatar
sletz committed
754
    JackNetMaster* JackNetMasterManager::InitMaster(session_params_t& params)
755
    {
sletz's avatar
sletz committed
756
        jack_log("JackNetMasterManager::InitMaster, Slave : %s", params.fName);
sletz's avatar
sletz committed
757

758
759
        //check MASTER <<==> SLAVE network protocol coherency
        if (params.fProtocolVersion != MASTER_PROTOCOL) {
760
            jack_error ( "Error : slave %s is running with a different protocol %d != %d", params.fName,  params.fProtocolVersion, MASTER_PROTOCOL);
761
762
            return NULL;
        }
moret's avatar
moret committed
763

764
765
766
767
768
        //settings
        fSocket.GetName ( params.fMasterNetName );
        params.fID = ++fGlobalID;
        params.fSampleRate = jack_get_sample_rate ( fManagerClient );
        params.fPeriodSize = jack_get_buffer_size ( fManagerClient );
769
770

        if (params.fSendAudioChannels == -1) {
sletz's avatar
sletz committed
771
772
            params.fSendAudioChannels = CountIO(JackPortIsPhysical | JackPortIsOutput);
            jack_info("Takes physical %d inputs for client", params.fSendAudioChannels);
773
774
775
        }

        if (params.fReturnAudioChannels == -1) {
sletz's avatar
sletz committed
776
            params.fReturnAudioChannels = CountIO(JackPortIsPhysical | JackPortIsInput);
777
778
779
            jack_info("Takes physical %d outputs for client", params.fReturnAudioChannels);
        }

sletz's avatar
sletz committed
780
        SetSlaveName(params);
781
782

        //create a new master and add it to the list
783
        JackNetMaster* master = new JackNetMaster(fSocket, params, fMulticastIP);
784
785
        if (master->Init(fAutoConnect)) {
            fMasterList.push_back(master);
786
787
788
789
790
791
792
793
794
            return master;
        }
        delete master;
        return NULL;
    }

    void JackNetMasterManager::SetSlaveName ( session_params_t& params )
    {
        jack_log ( "JackNetMasterManager::SetSlaveName" );
moret's avatar
moret committed
795

796
797
798
799
800
801
802
803
        master_list_it_t it;
        for ( it = fMasterList.begin(); it != fMasterList.end(); it++ )
            if ( strcmp ( ( *it )->fParams.fName, params.fName ) == 0 )
                sprintf ( params.fName, "%s-%u", params.fName, params.fID );
    }

    master_list_it_t JackNetMasterManager::FindMaster ( uint32_t id )
    {
sletz's avatar
sletz committed
804
        jack_log ( "JackNetMasterManager::FindMaster, ID %u", id );
moret's avatar
moret committed
805

806
807
808
809
810
811
812
        master_list_it_t it;
        for ( it = fMasterList.begin(); it != fMasterList.end(); it++ )
            if ( ( *it )->fParams.fID == id )
                return it;
        return it;
    }

813
    int JackNetMasterManager::KillMaster ( session_params_t* params )
814
    {
sletz's avatar
sletz committed
815
        jack_log ( "JackNetMasterManager::KillMaster, ID %u", params->fID );
moret's avatar
moret committed
816

817
818
819
820
821
        master_list_it_t master = FindMaster ( params->fID );
        if ( master != fMasterList.end() )
        {
            fMasterList.erase ( master );
            delete *master;
822
            return 1;
823
        }
824
        return 0;
825
826
827
828
829
830
831
832
833
834
    }
}//namespace

static Jack::JackNetMasterManager* master_manager = NULL;

#ifdef __cplusplus
extern "C"
{
#endif

835
    SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
836
    {
837
838
839
840
841
842
843
844
845
846
847
848
849
850
        jack_driver_desc_t * desc;
        jack_driver_desc_filler_t filler;
        jack_driver_param_value_t value;

        desc = jack_driver_descriptor_construct("netmanager", "netjack multi-cast master component", &filler);

        strcpy(value.str, DEFAULT_MULTICAST_IP );
        jack_driver_descriptor_add_parameter(desc, &filler, "multicast_ip", 'a', JackDriverParamString, &value, NULL, "Multicast Address", NULL);

        value.i = DEFAULT_PORT;
        jack_driver_descriptor_add_parameter(desc, &filler, "udp_net_port", 'p', JackDriverParamInt, &value, NULL, "UDP port", NULL);

        value.i = false;
        jack_driver_descriptor_add_parameter(desc, &filler, "auto_connect", 'c', JackDriverParamBool, &value, NULL, "Auto connect netmaster to system ports", NULL);
851
852
853
854

        return desc;
    }

855
    SERVER_EXPORT int jack_internal_initialize ( jack_client_t* jack_client, const JSList* params )
856
857
858
859
860
861
862
863
864
865
866
867
868
869
    {
        if ( master_manager )
        {
            jack_error ( "Master Manager already loaded" );
            return 1;
        }
        else
        {
            jack_log ( "Loading Master Manager" );
            master_manager = new Jack::JackNetMasterManager ( jack_client, params );
            return ( master_manager ) ? 0 : 1;
        }
    }

870
    SERVER_EXPORT int jack_initialize ( jack_client_t* jack_client, const char* load_init )
871
872
    {
        JSList* params = NULL;
873
874
        bool parse_params = true;
        int res = 1;
875
        jack_driver_desc_t* desc = jack_get_descriptor();
876
877
878

        Jack::JackArgParser parser ( load_init );
        if ( parser.GetArgc() > 0 )
879
            parse_params = parser.ParseParams ( desc, &params );
880

881
882
883
884
        if (parse_params) {
            res = jack_internal_initialize ( jack_client, params );
            parser.FreeParams ( params );
        }
sletz's avatar
sletz committed
885
        return res;
886
887
    }

888
    SERVER_EXPORT void jack_finish ( void* arg )
889
    {
890
891
        if ( master_manager )
        {
892
893
894
895
896
897
898
899
            jack_log ( "Unloading Master Manager" );
            delete master_manager;
            master_manager = NULL;
        }
    }
#ifdef __cplusplus
}
#endif