File: test-ipcclient.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 (331 lines) | stat: -rw-r--r-- 10,305 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
/*
 * 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/>.
 *
 */

#include "debugmodule/debugmodule.h"

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

#include <argp.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <signal.h>

#include <math.h>

using namespace Util;

DECLARE_GLOBAL_DEBUG_MODULE;

#define MAX_ARGS 2

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

////////////////////////////////////////////////
// arg parsing
////////////////////////////////////////////////
const char *argp_program_version = "test-ipcringbuffer 0.1";
const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
static char doc[] = "test-avccmd -- test program to test the ipc ringbuffer class.";
static char args_doc[] = "";
static struct argp_option options[] = {
    {"verbose",  'v', "level",    0,  "Produce verbose output" },
    {"playback", 'o', "count",    0,  "Number of playback channels" },
    {"capture",  'i', "count",    0,  "Number of capture channels" },
    {"period",  'p', "frames",    0,  "Period size in frames" },
    {"nb_buffers",  'n', "count",    0,  "Number of IPC buffers" },
  { 0 }
};

struct arguments
{
    arguments()
        : nargs ( 0 )
        , verbose( false )
        , playback( 0 )
        , capture( 0 )
        {
            args[0] = 0;
        }

    char* args[MAX_ARGS];
    int   nargs;
    long int verbose;
    long int playback;
    long int capture;
    long int period;
    long int nb_buffers;
    long int test_tone;
    float test_tone_freq;
} arguments;

// 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 '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 '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 'p':
        if (arg) {
            arguments->period = strtol( arg, &tail, 0 );
            if ( errno ) {
                fprintf( stderr,  "Could not parse 'periods' 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 ARGP_KEY_ARG:
        if (state->arg_num >= MAX_ARGS) {
            // Too many arguments.
            argp_usage (state);
        }
        arguments->args[state->arg_num] = arg;
        arguments->nargs++;
        break;
    case ARGP_KEY_END:
//         if(arguments->nargs <= 0) {
//             printMessage("not enough arguments\n");
//             return -1;
//         }
        break;
    default:
        return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc };

///////////////////////////
// main
//////////////////////////
int
main(int argc, char **argv)
{
    signal (SIGINT, sighandler);
    signal (SIGPIPE, sighandler);

    arguments.verbose           = 6;
    arguments.period            = 1024;
    arguments.nb_buffers        = 3;
    arguments.playback          = 0;
    arguments.capture           = 0;
    arguments.test_tone_freq    = 0.01;
    arguments.test_tone         = 1;

    // arg parsing
    if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
        fprintf( stderr, "Could not parse command line\n" );
        exit(-1);
    }

    setDebugLevel(arguments.verbose);

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

    printMessage("Testing shared memory streaming IPC\n");
    printMessage(" period %ld, nb_buffers %ld, playback %ld, capture %ld\n",
                 arguments.period, arguments.nb_buffers,
                 arguments.playback,
                 arguments.capture );

    // prepare the IPC buffers
    unsigned int capture_buffsize = arguments.capture * arguments.period * 4;
    unsigned int playback_buffsize = arguments.playback * arguments.period * 4;
    IpcRingBuffer* capturebuffer = NULL;
    IpcRingBuffer* playbackbuffer = NULL;
    if(arguments.playback) {
        playbackbuffer = new IpcRingBuffer("playbackbuffer",
                              IpcRingBuffer::eBT_Slave,
                              IpcRingBuffer::eD_Outward,
                              IpcRingBuffer::eB_Blocking,
                              arguments.nb_buffers, playback_buffsize);
        if(playbackbuffer == NULL) {
            debugError("Could not create playbackbuffer\n");
            exit(-1);
        }
        if(!playbackbuffer->init()) {
            debugError("Could not init playbackbuffer\n");
            delete playbackbuffer;
            exit(-1);
        }
        playbackbuffer->setVerboseLevel(arguments.verbose);
    }
    if(arguments.capture) {
        capturebuffer = new IpcRingBuffer("capturebuffer",
                              IpcRingBuffer::eBT_Slave,
                              IpcRingBuffer::eD_Inward,
                              IpcRingBuffer::eB_Blocking,
                              arguments.nb_buffers, capture_buffsize);
        if(capturebuffer == NULL) {
            debugError("Could not create capturebuffer\n");
            delete playbackbuffer;
            exit(-1);
        }
        if(!capturebuffer->init()) {
            debugError("Could not init capturebuffer\n");
            delete playbackbuffer;
            delete capturebuffer;
            exit(-1);
        }
        capturebuffer->setVerboseLevel(arguments.verbose);
    }


    // dummy buffers
    char capture_buff[capture_buffsize];
    char playback_buff[playback_buffsize];

    int cnt = 0;
    int pbkcnt = 0;

    float frame_counter = 0.0;
    float sine_advance = 0.0;
    float amplitude = 0.97;
    sine_advance = 2.0*M_PI*arguments.test_tone_freq;
    uint32_t sine_buff[arguments.period];

    run=1;
    while(run) {
        // test tone generation
        if (arguments.test_tone) {
            // generate the test tone
            for (int i=0; i < arguments.period; i++) {
                float v = amplitude * sin(sine_advance * (frame_counter + (float)i));
                v = (v * 2147483392.0);
                int32_t tmp = ((int) v);
                tmp = tmp >> 8;
                memcpy(&sine_buff[i], &tmp, 4);
            }
            for(int j=0; j<arguments.playback; j++) {
                uint32_t *target = (uint32_t *)(playback_buff + j*arguments.period*4);
                memcpy(target, &sine_buff, arguments.period*4);
            }
        }
        frame_counter += arguments.period;

        // write the data
        IpcRingBuffer::eResult res;
        if(playbackbuffer) {
            res = playbackbuffer->Write(playback_buff);
            if(res != IpcRingBuffer::eR_OK && res != IpcRingBuffer::eR_Again) {
                debugError("Could not write to segment\n");
                goto out_err;
            }
            if(res == IpcRingBuffer::eR_Again) {
                printMessage(" Try playback again on %d...\n", cnt);
            } else {
                if(pbkcnt%100==0) {
                    printMessage(" Period %d...\n", pbkcnt);
                }
                pbkcnt++;
            }
        }
        // read data
        if (capturebuffer) {
            res = capturebuffer->Read(capture_buff);
            if(res != IpcRingBuffer::eR_OK && res != IpcRingBuffer::eR_Again) {
                debugError("Could not receive from queue\n");
                goto out_err;
            }
            if(res == IpcRingBuffer::eR_Again) {
                printMessage(" Try again on %d...\n", cnt);
            } else {
                if(cnt%10==0) {
                    uint32_t *tmp = (uint32_t *)capture_buff;
                    for(int i=0;i<arguments.capture;i++) {
                        printMessage(" channel %d: ", i);
                        for(int j=0; j < 6;j+=1) {
                            uint32_t tmp2 = tmp[j] << 8;
                            int32_t *tmp3 = (int32_t *)&tmp2;
                            printMessageShort("%10d ", *tmp3);
                        }
                        tmp += arguments.period;
                        printMessageShort("\n");
                    }
                }
                cnt++;
            }
        }
    }

    delete capturebuffer;
    delete playbackbuffer;
    return 0;

out_err:
    delete capturebuffer;
    delete playbackbuffer;
    return -1;
}