File: teststreaming-ipc.cpp

package info (click to toggle)
libffado 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,320 kB
  • ctags: 11,892
  • sloc: cpp: 75,412; python: 8,911; ansic: 2,951; sh: 1,261; xml: 822; makefile: 54
file content (623 lines) | stat: -rw-r--r-- 21,831 bytes parent folder | download | duplicates (6)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 * Copyright (C) 2005-2008 by Pieter Palmers
 *
 * This file is part of FFADO
 * FFADO = Free Firewire (pro-)audio drivers for linux
 *
 * FFADO is based upon FreeBoB.
 *
 * 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) version 3 of the License.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */


/**
 * Test application for the IPC audio server
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <signal.h>
#include <sched.h>

#include "libffado/ffado.h"

#include "debugmodule/debugmodule.h"

#include "libutil/IpcRingBuffer.h"
#include "libutil/SystemTimeSource.h"

#include <math.h>
#include <argp.h>

int run;

DECLARE_GLOBAL_DEBUG_MODULE;

using namespace Util;

// Program documentation.
// Program documentation.
static char doc[] = "FFADO -- a driver for Firewire Audio devices (IPC streaming test application)\n\n"
                    ;

// A description of the arguments we accept.
static char args_doc[] = "";

struct arguments
{
    long int verbose;
    long int test_tone;
    long int test_tone_freq;
    long int period;
    long int slave_mode;
    long int snoop_mode;
    long int nb_buffers;
    long int sample_rate;
    long int rtprio;
    long int audio_buffer_type;
    long int playback;
    long int capture;
    char* args[2];
    
};

// The options we understand.
static struct argp_option options[] = {
    {"verbose",  'v', "level",    0,  "Verbose level" },
    {"rtprio",  'P', "prio",  0,  "Realtime priority (0 = no RT scheduling)" },
    {"test-tone",  't', "bool",  0,  "Output test sine" },
    {"test-tone-freq",  'f', "hz",  0,  "Test sine frequency" },
    {"samplerate",  'r', "hz",  0,  "Sample rate" },
    {"period",  'p', "frames",  0,  "Period (buffer) size" },
    {"nb_buffers",  'n', "nb",  0,  "Nb buffers (periods)" },
    {"slave_mode",  's', "bool",  0,  "Run in slave mode" },
    {"snoop_mode",  'S', "bool",  0,  "Run in snoop mode" },
    {"audio_buffer_type",  'b', "",  0,  "Datatype of audio buffers (0=float, 1=int24)" },
    {"playback",  'o', "",  0,  "Number of playback channels" },
    {"capture",  'i', "",  0,  "Number of capture channels" },
    { 0 }
};

//-------------------------------------------------------------

// Parse a single option.
static error_t
parse_opt( int key, char* arg, struct argp_state* state )
{
    // Get the input argument from `argp_parse', which we
    // know is a pointer to our arguments structure.
    struct arguments* arguments = ( struct arguments* ) state->input;
    char* tail;

    errno = 0;
    switch (key) {
    case 'v':
        if (arg) {
            arguments->verbose = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'verbose' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'P':
        if (arg) {
            arguments->rtprio = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'rtprio' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'p':
        if (arg) {
            arguments->period = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'period' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'n':
        if (arg) {
            arguments->nb_buffers = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'nb_buffers' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'r':
        if (arg) {
            arguments->sample_rate = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'samplerate' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 't':
        if (arg) {
            arguments->test_tone = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'test-tone' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'f':
        if (arg) {
            arguments->test_tone_freq = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'test-tone-freq' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'b':
        if (arg) {
            arguments->audio_buffer_type = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'audio-buffer-type' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'i':
        if (arg) {
            arguments->capture = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'capture' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'o':
        if (arg) {
            arguments->playback = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'playback' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 's':
        if (arg) {
            arguments->slave_mode = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'slave_mode' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case 'S':
        if (arg) {
            arguments->snoop_mode = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'snoop_mode' argument\n" );
                return ARGP_ERR_UNKNOWN;
            }
        }
        break;
    case ARGP_KEY_ARG:
        break;
    case ARGP_KEY_END:
        break;
    default:
        return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

// Our argp parser.
static struct argp argp = { options, parse_opt, args_doc, doc };

int set_realtime_priority(unsigned int prio)
{
    debugOutput(DEBUG_LEVEL_NORMAL, "Setting thread prio to %u\n", prio);
    if (prio > 0) {
        struct sched_param schp;
        /*
        * set the process to realtime privs
        */
        memset(&schp, 0, sizeof(schp));
        schp.sched_priority = prio;
        
        if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) {
            perror("sched_setscheduler");
            return -1;
        }
  } else {
        struct sched_param schp;
        /*
        * set the process to realtime privs
        */
        memset(&schp, 0, sizeof(schp));
        schp.sched_priority = 0;
        
        if (sched_setscheduler(0, SCHED_OTHER, &schp) != 0) {
            perror("sched_setscheduler");
            return -1;
        }
  }
  return 0;
}

static void sighandler (int sig)
{
    run = 0;
}

int main(int argc, char *argv[])
{

    struct arguments arguments;

    // Default values.
    arguments.verbose           = 6;
    arguments.period            = 1024;
    arguments.slave_mode        = 0;
    arguments.snoop_mode        = 0;
    arguments.nb_buffers        = 3;
    arguments.sample_rate       = 44100;
    arguments.rtprio            = 0;
    arguments.audio_buffer_type = 1;
    arguments.playback          = 0;
    arguments.capture           = 0;

    // Parse our arguments; every option seen by `parse_opt' will
    // be reflected in `arguments'.
    if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
        debugError("Could not parse command line\n" );
        return -1;
    }

    debugOutput(DEBUG_LEVEL_NORMAL, "verbose level = %ld\n", arguments.verbose);
    setDebugLevel(arguments.verbose);

    if(arguments.playback == 0 && arguments.capture == 0) {
        debugError("No playback nor capture channels requested\n");
        return -1;
    }

    int nb_in_channels=0, nb_out_channels=0;
    int i=0;
    int start_flag = 0;

    int nb_periods=0;

    uint32_t **audiobuffers_in = NULL;
    uint32_t **audiobuffers_out = NULL;
    float *nullbuffer = NULL;

    run=1;

    debugOutput(DEBUG_LEVEL_NORMAL, "FFADO streaming test application (3)\n");
    printMessage(" period %ld, nb_buffers %ld, playback %ld, capture %ld\n",
                 arguments.period, arguments.nb_buffers,
                 arguments.playback,
                 arguments.capture );

    signal (SIGINT, sighandler);
    signal (SIGPIPE, sighandler);

    ffado_device_info_t device_info;
    memset(&device_info,0,sizeof(ffado_device_info_t));

    ffado_options_t dev_options;
    memset(&dev_options,0,sizeof(ffado_options_t));

    dev_options.sample_rate = arguments.sample_rate;
    dev_options.period_size = arguments.period;

    dev_options.nb_buffers = arguments.nb_buffers;

    dev_options.realtime = (arguments.rtprio != 0);
    dev_options.packetizer_priority = arguments.rtprio;
    
    dev_options.verbose = arguments.verbose;
        
    dev_options.slave_mode = arguments.slave_mode;
    dev_options.snoop_mode = arguments.snoop_mode;

    ffado_device_t *dev=ffado_streaming_init(device_info, dev_options);

    if (!dev) {
        debugError("Could not init Ffado Streaming layer\n");
        exit(-1);
    }
    if (arguments.audio_buffer_type == 0) {
        ffado_streaming_set_audio_datatype(dev, ffado_audio_datatype_float);
    } else {
        ffado_streaming_set_audio_datatype(dev, ffado_audio_datatype_int24);
    }

    int nb_in_channels_all = ffado_streaming_get_nb_capture_streams(dev);
    int nb_out_channels_all = ffado_streaming_get_nb_playback_streams(dev);

    // only do audio for now
    for (i=0; i < nb_in_channels_all; i++) {
        switch (ffado_streaming_get_capture_stream_type(dev,i)) {
            case ffado_stream_type_audio:
                nb_in_channels++;
                break;
            case ffado_stream_type_midi:
            default:
                break;
        }
    }
    for (i=0; i < nb_out_channels_all; i++) {
        switch (ffado_streaming_get_playback_stream_type(dev,i)) {
            case ffado_stream_type_audio:
                nb_out_channels++;
                break;
            case ffado_stream_type_midi:
            default:
                break;
        }
    }

    printMessage("Device channel count: %d capture, %d playback\n",
                 nb_in_channels, nb_out_channels);
    printMessage("Requested channel count: %ld capture, %ld playback\n",
                 arguments.capture, arguments.playback);

    if(arguments.playback > nb_out_channels) {
        debugError("Too many playback channels requested (want: %ld, have:%d)\n", 
                   arguments.playback, nb_out_channels);
        return -1;
    }
    if(arguments.capture > nb_in_channels) {
        debugError("Too many capture channels requested (want: %ld, have:%d)\n", 
                   arguments.capture, nb_in_channels);
        return -1;
    }

    printMessage("Buffer size: %d capture, %d playback\n", 
                  nb_in_channels*dev_options.period_size * 4,
                  nb_out_channels*dev_options.period_size * 4);

    // allocate the IPC structures
    IpcRingBuffer* capturebuffer = NULL;
    IpcRingBuffer* playbackbuffer = NULL;
    if(arguments.capture) {
        // 4 bytes per channel per sample
        capturebuffer = new IpcRingBuffer("capturebuffer",
                                IpcRingBuffer::eBT_Master,
                                IpcRingBuffer::eD_Outward,
                                IpcRingBuffer::eB_NonBlocking,
                                arguments.nb_buffers,
                                dev_options.period_size * arguments.capture * 4);
        if(capturebuffer == NULL) {
            debugError("Could not create capture IPC buffer\n");
            exit(-1);
        }
    
        capturebuffer->setVerboseLevel(arguments.verbose);
    
        if(!capturebuffer->init()) {
            debugError("Could not init capture buffer\n");
            delete capturebuffer;
            exit(-1);
        }
        // indexes to the memory locations where the frames should be put
        audiobuffers_in = (uint32_t **)calloc(arguments.capture, sizeof(uint32_t *));
    }

    if(arguments.playback) {
        // 4 bytes per channel per sample
        playbackbuffer = new IpcRingBuffer("playbackbuffer",
                                IpcRingBuffer::eBT_Master,
                                IpcRingBuffer::eD_Inward,
                                IpcRingBuffer::eB_NonBlocking,
                                arguments.nb_buffers,
                                dev_options.period_size * arguments.playback * 4);
        if(playbackbuffer == NULL) {
            debugError("Could not create playback IPC buffer\n");
            exit(-1);
        }
    
        playbackbuffer->setVerboseLevel(arguments.verbose);
    
        if(!playbackbuffer->init()) {
            debugError("Could not init playback buffer\n");
            delete capturebuffer;
            delete playbackbuffer;
            exit(-1);
        }
        // indexes to the memory locations where the frames should be get
        audiobuffers_out = (uint32_t **)calloc(arguments.playback, sizeof(uint32_t *));
    }

    // this serves in case we miss a cycle, or a channel is disabled
    nullbuffer = (float *)calloc(arguments.period, sizeof(float));

    // give us RT prio
    set_realtime_priority(arguments.rtprio);

    // start the streaming layer
    if (ffado_streaming_prepare(dev)) {
        debugFatal("Could not prepare streaming system\n");
        ffado_streaming_finish(dev);
        return -1;
    }
    start_flag = ffado_streaming_start(dev);

    // enter the loop
    debugOutput(DEBUG_LEVEL_NORMAL,
                "Entering receive loop (IN: %ld, OUT: %ld)\n",
                arguments.capture, arguments.playback);

    while(run && start_flag==0) {
        bool need_silent;
        enum IpcRingBuffer::eResult msg_res;

        ffado_wait_response response;
        response = ffado_streaming_wait(dev);
        if (response == ffado_wait_xrun) {
            debugOutput(DEBUG_LEVEL_NORMAL, "Xrun\n");
            ffado_streaming_reset(dev);
            continue;
        } else if (response == ffado_wait_error) {
            debugError("fatal xrun\n");
            break;
        }

        // get a block pointer from the IPC buffer to write
        if(arguments.capture) {
            uint32_t *audiobuffers_raw;
            msg_res = capturebuffer->requestBlockForWrite((void**) &audiobuffers_raw); // pointer voodoo
            if(msg_res == IpcRingBuffer::eR_OK) {
                // if we got a valid pointer, setup the stream pointers
                for (i=0; i < nb_in_channels; i++) {
                    if(i < arguments.capture) {
                        audiobuffers_in[i] = audiobuffers_raw + i*dev_options.period_size;
                        switch (ffado_streaming_get_capture_stream_type(dev,i)) {
                            case ffado_stream_type_audio:
                                /* assign the audiobuffer to the stream */
                                ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(audiobuffers_in[i]));
                                ffado_streaming_capture_stream_onoff(dev, i, 1);
                                break;
                                // this is done with read/write routines because the nb of bytes can differ.
                            case ffado_stream_type_midi:
                                ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(audiobuffers_in[i]));
                                ffado_streaming_capture_stream_onoff(dev, i, 1);
                            default:
                                break;
                        }
                    } else {
                        ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_capture_stream_onoff(dev, i, 0);
                    }
                }
                need_silent = false;
            } else  {
                need_silent = true;
                debugOutput(DEBUG_LEVEL_NORMAL, "CAP: missed period %d\n", nb_periods);
            }
        } else {
            need_silent=true;
        }
        if(need_silent) {
            // if not, use the null buffer
            for (i=0; i < nb_in_channels; i++) {
                switch (ffado_streaming_get_capture_stream_type(dev,i)) {
                    case ffado_stream_type_audio:
                        /* assign the audiobuffer to the stream */
                        ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_capture_stream_onoff(dev, i, 0);
                        break;
                        // this is done with read/write routines because the nb of bytes can differ.
                    case ffado_stream_type_midi:
                        ffado_streaming_set_capture_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_capture_stream_onoff(dev, i, 0);
                    default:
                        break;
                }
            }
        }

        // transfer
        ffado_streaming_transfer_capture_buffers(dev);

        if(capturebuffer && !need_silent && msg_res == IpcRingBuffer::eR_OK) {
            // if we had a good block, release it
            // FIXME: we should check for errors here
            capturebuffer->releaseBlockForWrite();
        }

        if(arguments.playback) {
            uint32_t *audiobuffers_raw;
            // get a block pointer from the IPC buffer to read
            msg_res = playbackbuffer->requestBlockForRead((void**) &audiobuffers_raw); // pointer voodoo
            if(msg_res == IpcRingBuffer::eR_OK) {
                // if we got a valid pointer, setup the stream pointers
                for (i=0; i < nb_out_channels; i++) {
                    if(i < arguments.playback) {
                        audiobuffers_out[i] = audiobuffers_raw + i*dev_options.period_size;
                        switch (ffado_streaming_get_playback_stream_type(dev,i)) {
                            case ffado_stream_type_audio:
                                /* assign the audiobuffer to the stream */
                                ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(audiobuffers_out[i]));
                                ffado_streaming_playback_stream_onoff(dev, i, 1);
                                break;
                                // this is done with read/write routines because the nb of bytes can differ.
                            case ffado_stream_type_midi:
                                ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(audiobuffers_out[i]));
                                ffado_streaming_playback_stream_onoff(dev, i, 1);
                            default:
                                break;
                        }
                    } else {
                        ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_playback_stream_onoff(dev, i, 0);
                    }
                }
                need_silent=false;
            } else {
                debugOutput(DEBUG_LEVEL_NORMAL, "PBK: missed period %d\n", nb_periods);
                need_silent=true;
            }
        } else {
            need_silent=true;
        }
        if(need_silent) {
            // if not, use the null buffer
            memset(nullbuffer, 0, arguments.period * sizeof(float)); // clean it first
            for (i=0; i < nb_out_channels; i++) {
                switch (ffado_streaming_get_playback_stream_type(dev,i)) {
                    case ffado_stream_type_audio:
                        /* assign the audiobuffer to the stream */
                        ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_playback_stream_onoff(dev, i, 0);
                        break;
                        // this is done with read/write routines because the nb of bytes can differ.
                    case ffado_stream_type_midi:
                        ffado_streaming_set_playback_stream_buffer(dev, i, (char *)(nullbuffer));
                        ffado_streaming_playback_stream_onoff(dev, i, 0);
                    default:
                        break;
                }
            }
        }

        // transfer playback buffers
        ffado_streaming_transfer_playback_buffers(dev);

        if(playbackbuffer && !need_silent && msg_res == IpcRingBuffer::eR_OK) {
            // if we had a good block, release it
            // FIXME: we should check for errors here
            playbackbuffer->releaseBlockForRead();
        }

        nb_periods++;
    }

    debugOutput(DEBUG_LEVEL_NORMAL, "Exiting receive loop\n");

    ffado_streaming_stop(dev);
    ffado_streaming_finish(dev);

    delete capturebuffer;
    delete playbackbuffer;

    free(nullbuffer);
    free(audiobuffers_in);
    free(audiobuffers_out);

  return EXIT_SUCCESS;
}