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

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 "JackNetAdapter.h"
moret's avatar
moret committed
21
#include "JackException.h"
22
23
#include "JackServer.h"
#include "JackEngineControl.h"
24
25
26

#define DEFAULT_MULTICAST_IP "225.3.19.154"
#define DEFAULT_PORT 19000
sletz's avatar
sletz committed
27

28
29
namespace Jack
{
30
    JackNetAdapter::JackNetAdapter ( jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params )
moret's avatar
moret committed
31
            : JackAudioAdapterInterface ( buffer_size, sample_rate ), JackNetSlaveInterface(), fThread ( this )
32
33
    {
        jack_log ( "JackNetAdapter::JackNetAdapter" );
sletz's avatar
sletz committed
34

35
36
        if ( SocketAPIInit() < 0 )
            jack_error ( "Can't init Socket API, exiting..." );
moret's avatar
moret committed
37

moret's avatar
moret committed
38
        //global parametering
39
40
        fMulticastIP = new char[16];
        strcpy ( fMulticastIP, DEFAULT_MULTICAST_IP );
moret's avatar
moret committed
41
        uint port = DEFAULT_PORT;
42
43
44
45
46
47
48
49
        GetHostName ( fParams.fName, JACK_CLIENT_NAME_SIZE );
        fSocket.GetName ( fParams.fSlaveNetName );
        fParams.fMtu = 1500;
        fParams.fTransportSync = 1;
        fParams.fSendAudioChannels = 2;
        fParams.fReturnAudioChannels = 2;
        fParams.fSendMidiChannels = 0;
        fParams.fReturnMidiChannels = 0;
moret's avatar
moret committed
50
51
        fParams.fSampleRate = sample_rate;
        fParams.fPeriodSize = buffer_size;
moret's avatar
Cleanup    
moret committed
52
        fParams.fSlaveSyncMode = 1;
53
        fParams.fNetworkMode = 'n';
sletz's avatar
sletz committed
54

moret's avatar
moret committed
55
        //options parsing
56
57
58
59
60
61
62
        const JSList* node;
        const jack_driver_param_t* param;
        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
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
                case 'a' :
                    if ( strlen ( param->value.str ) < 16 )
                        strcpy ( fMulticastIP, param->value.str );
                    else
                        jack_error ( "Can't use multicast address %s, using default %s", param->value.ui, DEFAULT_MULTICAST_IP );
                    break;
                case 'p' :
                    fSocket.SetPort ( param->value.ui );
                    break;
                case 'M' :
                    fParams.fMtu = param->value.i;
                    break;
                case 'C' :
                    fParams.fSendAudioChannels = param->value.i;
                    break;
                case 'P' :
                    fParams.fReturnAudioChannels = param->value.i;
                    break;
                case 'n' :
                    strncpy ( fParams.fName, param->value.str, JACK_CLIENT_NAME_SIZE );
                    break;
                case 't' :
                    fParams.fTransportSync = param->value.ui;
                    break;
                case 'm' :
                    if ( strcmp ( param->value.str, "normal" ) == 0 )
                        fParams.fNetworkMode = 'n';
                    else if ( strcmp ( param->value.str, "slow" ) == 0 )
                        fParams.fNetworkMode = 's';
                    else if ( strcmp ( param->value.str, "fast" ) == 0 )
                        fParams.fNetworkMode = 'f';
                    else
                        jack_error ( "Unknown network mode, using 'normal' mode." );
                    break;
                case 'S' :
                    fParams.fSlaveSyncMode = 1;
                    break;
100
101
            }
        }
moret's avatar
moret committed
102

moret's avatar
moret committed
103
104
105
106
107
108
        fSocket.SetPort ( port );
        fSocket.SetAddress ( fMulticastIP, port );

        SetInputs ( fParams.fSendAudioChannels );
        SetOutputs ( fParams.fReturnAudioChannels );

109
110
111
        fSoftCaptureBuffer = NULL;
        fSoftPlaybackBuffer = NULL;
    }
sletz's avatar
sletz committed
112

113
114
    JackNetAdapter::~JackNetAdapter()
    {
115
        jack_log ( "JackNetAdapter::~JackNetAdapter" );
moret's avatar
moret committed
116

moret's avatar
moret committed
117
        int port_index;
moret's avatar
moret committed
118
119
        if ( fSoftCaptureBuffer )
        {
120
121
122
123
            for ( port_index = 0; port_index < fCaptureChannels; port_index++ )
                delete[] fSoftCaptureBuffer[port_index];
            delete[] fSoftCaptureBuffer;
        }
moret's avatar
moret committed
124
125
        if ( fSoftPlaybackBuffer )
        {
126
127
128
129
            for ( port_index = 0; port_index < fPlaybackChannels; port_index++ )
                delete[] fSoftPlaybackBuffer[port_index];
            delete[] fSoftPlaybackBuffer;
        }
moret's avatar
moret committed
130
    }
sletz's avatar
sletz committed
131

132
133
    int JackNetAdapter::Open()
    {
moret's avatar
moret committed
134
        jack_log ( "JackNetAdapter::Open" );
moret's avatar
moret committed
135

moret's avatar
moret committed
136
        jack_info ( "Net adapter started in %s mode %s Master's transport sync.",
moret's avatar
moret committed
137
                    ( fParams.fSlaveSyncMode ) ? "sync" : "async", ( fParams.fTransportSync ) ? "with" : "without" );
138

moret's avatar
moret committed
139
140
141
        if ( fThread.StartSync() < 0 )
        {
            jack_error ( "Cannot start netadapter thread" );
142
143
            return -1;
        }
moret's avatar
moret committed
144
145

        fThread.AcquireRealTime ( JackServer::fInstance->GetEngineControl()->fPriority - 1 );
146
        return 0;
moret's avatar
moret committed
147
    }
148
149
150

    int JackNetAdapter::Close()
    {
moret's avatar
moret committed
151
152
153
154
155
        jack_log ( "JackNetAdapter::Close" );

        switch ( fThread.GetStatus() )
        {
                // Kill the thread in Init phase
156
157
            case JackThread::kStarting:
            case JackThread::kIniting:
moret's avatar
moret committed
158
159
160
                if ( fThread.Kill() < 0 )
                {
                    jack_error ( "Cannot kill thread" );
161
162
163
                    return -1;
                }
                break;
moret's avatar
moret committed
164
                // Stop when the thread cycle is finished
165
            case JackThread::kRunning:
moret's avatar
moret committed
166
167
168
                if ( fThread.Stop() < 0 )
                {
                    jack_error ( "Cannot stop thread" );
169
170
171
172
173
174
                    return -1;
                }
                break;
            default:
                break;
        }
175
176
177
178
179
180
        fSocket.Close();
        return 0;
    }

    int JackNetAdapter::SetBufferSize ( jack_nframes_t buffer_size )
    {
moret's avatar
moret committed
181
        fParams.fPeriodSize = buffer_size;
182
183
184
185
186
        return 0;
    }

    bool JackNetAdapter::Init()
    {
moret's avatar
moret committed
187
        jack_log ( "JackNetAdapter::Init" );
188

moret's avatar
moret committed
189
190
191
192
193
194
195
196
197
198
199
200
        int port_index;

        //init network connection
        if ( !JackNetSlaveInterface::Init() )
            return false;

        //then set global parameters
        SetParams();

        //set buffers
        fSoftCaptureBuffer = new sample_t*[fCaptureChannels];
        for ( port_index = 0; port_index < fCaptureChannels; port_index++ )
moret's avatar
moret committed
201
        {
moret's avatar
moret committed
202
203
            fSoftCaptureBuffer[port_index] = new sample_t[fParams.fPeriodSize];
            fNetAudioCaptureBuffer->SetBuffer ( port_index, fSoftCaptureBuffer[port_index] );
moret's avatar
moret committed
204
        }
moret's avatar
moret committed
205
206
207
208
209
210
211
212
213
214
        fSoftPlaybackBuffer = new sample_t*[fPlaybackChannels];
        for ( port_index = 0; port_index < fCaptureChannels; port_index++ )
        {
            fSoftPlaybackBuffer[port_index] = new sample_t[fParams.fPeriodSize];
            fNetAudioPlaybackBuffer->SetBuffer ( port_index, fSoftPlaybackBuffer[port_index] );
        }

        //init done, display parameters
        SessionParamsDisplay ( &fParams );

moret's avatar
moret committed
215
        return true;
216
217
    }

moret's avatar
moret committed
218
    bool JackNetAdapter::Execute()
moret's avatar
moret committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    {
        try
        {
            // Keep running even in case of error
            while ( fThread.GetStatus() == JackThread::kRunning )
                if ( Process() == SOCKET_ERROR )
                    return false;
            return false;
        }
        catch ( JackNetException& e )
        {
            e.PrintMessage();
            jack_log ( "NetAdapter is restarted." );
            fThread.DropRealTime();
moret's avatar
moret committed
233
            fThread.SetStatus ( JackThread::kIniting );
moret's avatar
moret committed
234
235
236
237
238
239
240
241
242
243
            if ( Init() )
            {
                fThread.SetStatus ( JackThread::kRunning );
                return true;
            }
            else
                return false;
        }
    }

moret's avatar
moret committed
244
    int JackNetAdapter::Read()
245
    {
moret's avatar
moret committed
246
        if ( SyncRecv() == SOCKET_ERROR )
moret's avatar
moret committed
247
            return 0;
moret's avatar
moret committed
248

moret's avatar
moret committed
249
250
251
252
253
254
        return DataRecv();
    }

    int JackNetAdapter::Write()
    {
        if ( SyncSend() == SOCKET_ERROR )
moret's avatar
moret committed
255
            return SOCKET_ERROR;
moret's avatar
moret committed
256

moret's avatar
moret committed
257
258
        return DataSend();
    }
moret's avatar
moret committed
259

moret's avatar
moret committed
260
261
262
263
264
    int JackNetAdapter::Process()
    {
        bool failure = false;
        int port_index;
        int rx_bytes, tx_bytes;
moret's avatar
moret committed
265

moret's avatar
moret committed
266
267
268
269
270
        //read data from the network
        //in case of fatal network error, definitely stop the process
        rx_bytes = Read();
        if ( rx_bytes == SOCKET_ERROR )
            return SOCKET_ERROR;
moret's avatar
moret committed
271

moret's avatar
moret committed
272
273
        //if there is data to resample,
        if ( rx_bytes )
moret's avatar
moret committed
274
        {
moret's avatar
moret committed
275
276
277
            //get the resample factor,
            jack_nframes_t time1, time2;
            ResampleFactor ( time1, time2 );
moret's avatar
moret committed
278

moret's avatar
moret committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
            //resample input data,
            for ( port_index = 0; port_index < fCaptureChannels; port_index++ )
            {
                fCaptureRingBuffer[port_index]->SetRatio ( time1, time2 );
                if ( fCaptureRingBuffer[port_index]->WriteResample ( fSoftCaptureBuffer[port_index], fBufferSize ) < fBufferSize )
                    failure = true;
            }
            //and output data,
            for ( port_index = 0; port_index < fPlaybackChannels; port_index++ )
            {
                fPlaybackRingBuffer[port_index]->SetRatio ( time2, time1 );
                if ( fPlaybackRingBuffer[port_index]->ReadResample ( fSoftPlaybackBuffer[port_index], fBufferSize ) < fBufferSize )
                    failure = true;
            }
        }
moret's avatar
moret committed
294

moret's avatar
moret committed
295
296
297
        //then write data to network
        //in case of failure, definitely stop process
        if ( Write() == SOCKET_ERROR )
moret's avatar
moret committed
298
            return SOCKET_ERROR;
moret's avatar
moret committed
299

moret's avatar
moret committed
300
        //if there was any ringbuffer failure during resampling, reset
moret's avatar
moret committed
301
302
303
304
305
306
307
        if ( failure )
        {
            jack_error ( "JackNetAdapter::Execute ringbuffer failure...reset." );
            ResetRingBuffers();
        }

        return true;
308
    }
309
} // namespace Jack
sletz's avatar
sletz committed
310
311
312
313
314
315
316

#ifdef __cplusplus
extern "C"
{
#endif

#include "driver_interface.h"
317
318
#include "JackAudioAdapter.h"

moret's avatar
moret committed
319
    using namespace Jack;
sletz's avatar
sletz committed
320
321
322

    EXPORT jack_driver_desc_t* jack_get_descriptor()
    {
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
        jack_driver_desc_t* desc = ( jack_driver_desc_t* ) calloc ( 1, sizeof ( jack_driver_desc_t ) );
        strcpy ( desc->name, "net" );
        desc->nparams = 9;
        desc->params = ( jack_driver_param_desc_t* ) calloc ( desc->nparams, sizeof ( jack_driver_param_desc_t ) );

        int i = 0;
        strcpy ( desc->params[i].name, "multicast_ip" );
        desc->params[i].character = 'a';
        desc->params[i].type = JackDriverParamString;
        strcpy ( desc->params[i].value.str, DEFAULT_MULTICAST_IP );
        strcpy ( desc->params[i].short_desc, "Multicast Address" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "udp_net_port" );
        desc->params[i].character = 'p';
        desc->params[i].type = JackDriverParamInt;
        desc->params[i].value.i = 19000;
        strcpy ( desc->params[i].short_desc, "UDP port" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "mtu" );
        desc->params[i].character = 'M';
        desc->params[i].type = JackDriverParamInt;
        desc->params[i].value.i = 1500;
        strcpy ( desc->params[i].short_desc, "MTU to the master" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "input_ports" );
        desc->params[i].character = 'C';
        desc->params[i].type = JackDriverParamInt;
        desc->params[i].value.i = 2;
        strcpy ( desc->params[i].short_desc, "Number of audio input ports" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "output_ports" );
        desc->params[i].character = 'P';
        desc->params[i].type = JackDriverParamInt;
        desc->params[i].value.i = 2;
        strcpy ( desc->params[i].short_desc, "Number of audio output ports" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "client_name" );
        desc->params[i].character = 'n';
        desc->params[i].type = JackDriverParamString;
        strcpy ( desc->params[i].value.str, "'hostname'" );
        strcpy ( desc->params[i].short_desc, "Name of the jack client" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );

        i++;
        strcpy ( desc->params[i].name, "transport_sync" );
        desc->params[i].character  = 't';
        desc->params[i].type = JackDriverParamUInt;
        desc->params[i].value.ui = 1U;
        strcpy ( desc->params[i].short_desc, "Sync transport with master's" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
moret's avatar
moret committed
383

384
        i++;
moret's avatar
moret committed
385
386
        strcpy ( desc->params[i].name, "mode" );
        desc->params[i].character  = 'm';
387
        desc->params[i].type = JackDriverParamString;
moret's avatar
moret committed
388
389
        strcpy ( desc->params[i].value.str, "normal" );
        strcpy ( desc->params[i].short_desc, "Slow, Normal or Fast mode." );
390
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
moret's avatar
moret committed
391

392
393
394
395
396
397
398
        i++;
        strcpy ( desc->params[i].name, "sync_mode" );
        desc->params[i].character  = 'S';
        desc->params[i].type = JackDriverParamString;
        strcpy ( desc->params[i].value.str, "" );
        strcpy ( desc->params[i].short_desc, "Sync mode (same as driver's sync mode) ?" );
        strcpy ( desc->params[i].long_desc, desc->params[i].short_desc );
moret's avatar
moret committed
399

sletz's avatar
sletz committed
400
401
402
        return desc;
    }

moret's avatar
moret committed
403
    EXPORT int jack_internal_initialize ( jack_client_t* jack_client, const JSList* params )
sletz's avatar
sletz committed
404
    {
moret's avatar
moret committed
405
        jack_log ( "Loading netadapter" );
sletz's avatar
sletz committed
406
407

        Jack::JackAudioAdapter* adapter;
moret's avatar
moret committed
408
409
        jack_nframes_t buffer_size = jack_get_buffer_size ( jack_client );
        jack_nframes_t sample_rate = jack_get_sample_rate ( jack_client );
sletz's avatar
sletz committed
410

moret's avatar
moret committed
411
412
        adapter = new Jack::JackAudioAdapter ( jack_client, new Jack::JackNetAdapter ( buffer_size, sample_rate, params ) );
        assert ( adapter );
sletz's avatar
sletz committed
413

moret's avatar
moret committed
414
        if ( adapter->Open() == 0 )
sletz's avatar
sletz committed
415
            return 0;
moret's avatar
moret committed
416
417
        else
        {
sletz's avatar
sletz committed
418
419
420
421
422
            delete adapter;
            return 1;
        }
    }

moret's avatar
moret committed
423
    EXPORT int jack_initialize ( jack_client_t* jack_client, const char* load_init )
sletz's avatar
sletz committed
424
425
426
427
    {
        JSList* params = NULL;
        jack_driver_desc_t *desc = jack_get_descriptor();

moret's avatar
moret committed
428
        JackArgParser parser ( load_init );
sletz's avatar
sletz committed
429

moret's avatar
moret committed
430
431
432
        if ( parser.GetArgc() > 0 )
            if ( parser.ParseParams ( desc, &params ) != 0 )
                jack_error ( "Internal client : JackArgParser::ParseParams error." );
sletz's avatar
sletz committed
433

moret's avatar
moret committed
434
        return jack_internal_initialize ( jack_client, params );
sletz's avatar
sletz committed
435
436
    }

moret's avatar
moret committed
437
    EXPORT void jack_finish ( void* arg )
sletz's avatar
sletz committed
438
    {
moret's avatar
moret committed
439
        Jack::JackAudioAdapter* adapter = static_cast<Jack::JackAudioAdapter*> ( arg );
sletz's avatar
sletz committed
440

moret's avatar
moret committed
441
442
443
        if ( adapter )
        {
            jack_log ( "Unloading netadapter" );
sletz's avatar
sletz committed
444
445
446
447
448
449
450
451
            adapter->Close();
            delete adapter;
        }
    }

#ifdef __cplusplus
}
#endif