~ubuntu-branches/ubuntu/trusty/jack-audio-connection-kit/trusty

« back to all changes in this revision

Viewing changes to jack/internal.h

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-file-style: "bsd"; -*- */
 
2
/*
 
3
    Internal shared data and functions.
 
4
 
 
5
    If you edit this file, you should carefully consider changing the
 
6
    JACK_PROTOCOL_VERSION in configure.in.
 
7
 
 
8
    Copyright (C) 2001-2003 Paul Davis
 
9
    
 
10
    This program is free software; you can redistribute it and/or modify
 
11
    it under the terms of the GNU General Public License as published by
 
12
    the Free Software Foundation; either version 2 of the License, or
 
13
    (at your option) any later version.
 
14
 
 
15
    This program is distributed in the hope that it will be useful,
 
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
    GNU General Public License for more details.
 
19
 
 
20
    You should have received a copy of the GNU General Public License
 
21
    along with this program; if not, write to the Free Software
 
22
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
23
 
 
24
*/
 
25
 
 
26
#ifndef __jack_internal_h__
 
27
#define __jack_internal_h__
 
28
 
 
29
#include <stdlib.h>
 
30
#include <unistd.h>
 
31
#include <limits.h>
 
32
#include <dlfcn.h>
 
33
#include <pthread.h>
 
34
#include <sys/types.h>
 
35
#include <sys/time.h>
 
36
 
 
37
#ifndef POST_PACKED_STRUCTURE
 
38
#ifdef __GNUC__
 
39
/* POST_PACKED_STRUCTURE needs to be a macro which
 
40
   expands into a compiler directive. The directive must
 
41
   tell the compiler to arrange the preceding structure
 
42
   declaration so that it is packed on byte-boundaries rather 
 
43
   than use the natural alignment of the processor and/or
 
44
   compiler.
 
45
*/
 
46
#define POST_PACKED_STRUCTURE __attribute__((__packed__))
 
47
#else
 
48
/* Add other things here for non-gcc platforms */
 
49
#endif
 
50
#endif
 
51
 
 
52
/* Needed by <sysdeps/time.h> */
 
53
extern void jack_error (const char *fmt, ...);
 
54
 
 
55
extern void jack_info (const char *fmt, ...);
 
56
 
 
57
#include <jack/jack.h>
 
58
#include <jack/types.h>
 
59
#include <jack/port.h>
 
60
#include <jack/transport.h>
 
61
 
 
62
typedef enum {
 
63
        JACK_TIMER_SYSTEM_CLOCK,
 
64
        JACK_TIMER_CYCLE_COUNTER,
 
65
        JACK_TIMER_HPET,
 
66
} jack_timer_type_t;
 
67
 
 
68
void        jack_init_time ();
 
69
void        jack_set_clock_source (jack_timer_type_t);
 
70
const char* jack_clock_source_name (jack_timer_type_t);
 
71
 
 
72
#include <sysdeps/time.h>
 
73
#include <sysdeps/atomicity.h>
 
74
 
 
75
#ifdef JACK_USE_MACH_THREADS
 
76
#include <sysdeps/mach_port.h>
 
77
#endif
 
78
 
 
79
#include <jack/messagebuffer.h>
 
80
 
 
81
#ifdef DEBUG_ENABLED
 
82
 
 
83
/* grab thread id instead of PID on linux */
 
84
#if defined(__gnu_linux__)
 
85
    #ifdef gettid  /* glibc has a version */
 
86
        #define GETTID() gettid()
 
87
    #else /* use our own version */
 
88
        #include <sys/syscall.h>
 
89
        #define GETTID() syscall(__NR_gettid)
 
90
    #endif
 
91
#else
 
92
    #define GETTID() getpid()
 
93
#endif
 
94
 
 
95
#define DEBUG(format,args...) \
 
96
        MESSAGE("jack:%5d:%" PRIu64 " %s:%s:%d: " format "", GETTID(), jack_get_microseconds(), __FILE__, __FUNCTION__, __LINE__ , ## args)
 
97
 
 
98
#else
 
99
#if JACK_CPP_VARARGS_BROKEN
 
100
    #define DEBUG(format...)
 
101
#else
 
102
    #define DEBUG(format,args...)
 
103
#endif
 
104
#endif
 
105
 
 
106
/* Enable preemption checking for Linux Realtime Preemption kernels.
 
107
 *
 
108
 * This checks if any RT-safe code section does anything to cause CPU
 
109
 * preemption.  Examples are sleep() or other system calls that block.
 
110
 * If a problem is detected, the kernel writes a syslog entry, and
 
111
 * sends SIGUSR2 to the client.
 
112
 */
 
113
#ifdef DO_PREEMPTION_CHECKING
 
114
#define CHECK_PREEMPTION(engine, onoff) \
 
115
        if ((engine)->real_time) gettimeofday (1, (onoff))
 
116
#else
 
117
#define CHECK_PREEMPTION(engine, onoff)
 
118
#endif
 
119
 
 
120
#ifndef FALSE
 
121
#define FALSE   (0)
 
122
#endif
 
123
 
 
124
#ifndef TRUE
 
125
#define TRUE    (1)
 
126
#endif
 
127
 
 
128
typedef struct _jack_engine  jack_engine_t;
 
129
typedef struct _jack_request jack_request_t;
 
130
 
 
131
typedef void * dlhandle;
 
132
 
 
133
typedef enum {
 
134
    TransportCommandNone = 0,
 
135
    TransportCommandStart = 1,
 
136
    TransportCommandStop = 2,
 
137
} transport_command_t;
 
138
 
 
139
typedef struct {
 
140
 
 
141
        volatile uint32_t       guard1;
 
142
        volatile jack_nframes_t frames;  
 
143
        volatile jack_time_t    current_wakeup;
 
144
        volatile jack_time_t    next_wakeup;
 
145
        volatile float          second_order_integrator;
 
146
        volatile int32_t        initialized;
 
147
        volatile uint32_t       guard2;
 
148
        
 
149
        /* not accessed by clients */
 
150
 
 
151
        int32_t  reset_pending;      /* xrun happened, deal with it */
 
152
        float    filter_coefficient; /* set once, never altered */
 
153
 
 
154
} POST_PACKED_STRUCTURE jack_frame_timer_t;
 
155
 
 
156
/* JACK engine shared memory data structure. */
 
157
typedef struct {
 
158
 
 
159
    jack_transport_state_t transport_state;
 
160
    volatile transport_command_t transport_cmd;
 
161
    transport_command_t   previous_cmd; /* previous transport_cmd */
 
162
    jack_position_t       current_time; /* position for current cycle */
 
163
    jack_position_t       pending_time; /* position for next cycle */
 
164
    jack_position_t       request_time; /* latest requested position */
 
165
    jack_unique_t         prev_request; /* previous request unique ID */
 
166
    volatile _Atomic_word seq_number;   /* unique ID sequence number */
 
167
    int8_t                new_pos;      /* new position this cycle */
 
168
    int8_t                pending_pos;  /* new position request pending */
 
169
    jack_nframes_t        pending_frame; /* pending frame number */
 
170
    int32_t               sync_clients; /* number of active_slowsync clients */
 
171
    int32_t               sync_remain;  /* number of them with sync_poll */
 
172
    jack_time_t           sync_timeout;
 
173
    jack_time_t           sync_time_left;
 
174
    jack_frame_timer_t    frame_timer;
 
175
    int32_t               internal;
 
176
    jack_timer_type_t     clock_source;
 
177
    pid_t                 engine_pid;
 
178
    jack_nframes_t        buffer_size;
 
179
    int8_t                real_time;
 
180
    int8_t                do_mlock;
 
181
    int8_t                do_munlock;
 
182
    int32_t               client_priority;
 
183
    int32_t               max_client_priority;
 
184
    int32_t               has_capabilities;
 
185
    float                 cpu_load;
 
186
    float                 xrun_delayed_usecs;
 
187
    float                 max_delayed_usecs;
 
188
    uint32_t              port_max;
 
189
    int32_t               engine_ok;
 
190
    jack_port_type_id_t   n_port_types;
 
191
    jack_port_type_info_t port_types[JACK_MAX_PORT_TYPES];
 
192
    jack_port_shared_t    ports[0];
 
193
 
 
194
} POST_PACKED_STRUCTURE jack_control_t;
 
195
 
 
196
typedef enum  {
 
197
  BufferSizeChange,
 
198
  SampleRateChange,
 
199
  AttachPortSegment,
 
200
  PortConnected,
 
201
  PortDisconnected,
 
202
  GraphReordered,
 
203
  PortRegistered,
 
204
  PortUnregistered,
 
205
  XRun,
 
206
  StartFreewheel,
 
207
  StopFreewheel,
 
208
  ClientRegistered,
 
209
  ClientUnregistered
 
210
} JackEventType;
 
211
 
 
212
typedef struct {
 
213
    JackEventType type;
 
214
    union {
 
215
        uint32_t n;
 
216
        char name[JACK_CLIENT_NAME_SIZE];    
 
217
        jack_port_id_t port_id;
 
218
        jack_port_id_t self_id;
 
219
    } x;
 
220
    union {
 
221
        uint32_t n;
 
222
        jack_port_type_id_t ptid;
 
223
        jack_port_id_t other_id;
 
224
    } y;
 
225
} POST_PACKED_STRUCTURE jack_event_t;
 
226
 
 
227
typedef enum {
 
228
        ClientInternal, /* connect request just names .so */
 
229
        ClientDriver,   /* code is loaded along with driver */
 
230
        ClientExternal  /* client is in another process */
 
231
} ClientType;
 
232
 
 
233
typedef enum {
 
234
        NotTriggered,
 
235
        Triggered,
 
236
        Running,
 
237
        Finished
 
238
} jack_client_state_t;
 
239
 
 
240
/* JACK client shared memory data structure. */
 
241
typedef volatile struct {
 
242
 
 
243
    volatile jack_client_id_t id;         /* w: engine r: engine and client */
 
244
    volatile jack_nframes_t  nframes;     /* w: engine r: client */
 
245
    volatile jack_client_state_t state;   /* w: engine and client r: engine */
 
246
    volatile char       name[JACK_CLIENT_NAME_SIZE];
 
247
    volatile ClientType type;             /* w: engine r: engine and client */
 
248
    volatile int8_t     active;           /* w: engine r: engine and client */
 
249
    volatile int8_t     dead;             /* r/w: engine */
 
250
    volatile int8_t     timed_out;        /* r/w: engine */
 
251
    volatile int8_t     is_timebase;      /* w: engine, r: engine and client */
 
252
    volatile int8_t     timebase_new;     /* w: engine and client, r: engine */
 
253
    volatile int8_t     is_slowsync;      /* w: engine, r: engine and client */
 
254
    volatile int8_t     active_slowsync;  /* w: engine, r: engine and client */
 
255
    volatile int8_t     sync_poll;        /* w: engine and client, r: engine */
 
256
    volatile int8_t     sync_new;         /* w: engine and client, r: engine */
 
257
    volatile pid_t      pid;              /* w: client r: engine; client pid */
 
258
    volatile pid_t      pgrp;             /* w: client r: engine; client pgrp */
 
259
    volatile uint64_t   signalled_at;
 
260
    volatile uint64_t   awake_at;
 
261
    volatile uint64_t   finished_at;
 
262
    volatile int32_t    last_status;         /* w: client, r: engine and client */
 
263
 
 
264
    /* indicators for whether callbacks have been set for this client.
 
265
       We do not include ptrs to the callbacks here (or their arguments)
 
266
       so that we can avoid 32/64 bit pointer size mismatches between
 
267
       the jack server and a client. The pointers are in the client-
 
268
       local structure which is part of the libjack compiled for
 
269
       either 32 bit or 64 bit clients.
 
270
     */
 
271
    volatile uint8_t    process_cbset;
 
272
    volatile uint8_t    thread_init_cbset;
 
273
    volatile uint8_t    bufsize_cbset;
 
274
    volatile uint8_t    srate_cbset;
 
275
    volatile uint8_t    port_register_cbset;
 
276
    volatile uint8_t    port_connect_cbset;
 
277
    volatile uint8_t    graph_order_cbset;
 
278
    volatile uint8_t    xrun_cbset;
 
279
    volatile uint8_t    sync_cb_cbset;
 
280
    volatile uint8_t    timebase_cb_cbset;
 
281
    volatile uint8_t    freewheel_cb_cbset;
 
282
    volatile uint8_t    client_register_cbset;
 
283
    volatile uint8_t    thread_cb_cbset;
 
284
 
 
285
} POST_PACKED_STRUCTURE jack_client_control_t;
 
286
 
 
287
typedef struct {
 
288
    
 
289
    uint32_t    protocol_v;             /* protocol version, must go first */
 
290
    int32_t    load;
 
291
    ClientType type;
 
292
    jack_options_t options;
 
293
 
 
294
    char name[JACK_CLIENT_NAME_SIZE];
 
295
    char object_path[PATH_MAX+1];
 
296
    char object_data[1024];
 
297
 
 
298
} POST_PACKED_STRUCTURE jack_client_connect_request_t;
 
299
 
 
300
typedef struct {
 
301
 
 
302
    jack_status_t status;
 
303
 
 
304
    jack_shm_registry_index_t client_shm_index;
 
305
    jack_shm_registry_index_t engine_shm_index;
 
306
 
 
307
    char        fifo_prefix[PATH_MAX+1];
 
308
 
 
309
    int32_t     realtime;
 
310
    int32_t     realtime_priority;
 
311
 
 
312
    char name[JACK_CLIENT_NAME_SIZE];   /* unique name, if assigned */
 
313
 
 
314
    /* these two are valid only for internal clients, and thus
 
315
       are exempt from the requirement that we not export
 
316
       pointers back to clients. an internal client must
 
317
       necessarily match the host, so 32/64 bit issues
 
318
       do not apply to these pointers.
 
319
    */
 
320
    jack_client_control_t* client_control;
 
321
    jack_control_t* engine_control;
 
322
 
 
323
#ifdef JACK_USE_MACH_THREADS
 
324
    /* specific resources for server/client real-time thread communication */
 
325
    int32_t     portnum;
 
326
#endif
 
327
 
 
328
} POST_PACKED_STRUCTURE jack_client_connect_result_t;
 
329
 
 
330
typedef struct {
 
331
    jack_client_id_t client_id;
 
332
} POST_PACKED_STRUCTURE jack_client_connect_ack_request_t;
 
333
 
 
334
typedef struct {
 
335
    int8_t status;
 
336
} POST_PACKED_STRUCTURE jack_client_connect_ack_result_t;
 
337
 
 
338
typedef enum {
 
339
        RegisterPort = 1,
 
340
        UnRegisterPort = 2,
 
341
        ConnectPorts = 3,
 
342
        DisconnectPorts = 4, 
 
343
        SetTimeBaseClient = 5,
 
344
        ActivateClient = 6,
 
345
        DeactivateClient = 7,
 
346
        DisconnectPort = 8,
 
347
        SetClientCapabilities = 9,
 
348
        GetPortConnections = 10,
 
349
        GetPortNConnections = 11,
 
350
        ResetTimeBaseClient = 12,
 
351
        SetSyncClient = 13,
 
352
        ResetSyncClient = 14,
 
353
        SetSyncTimeout = 15,
 
354
        SetBufferSize = 16,
 
355
        FreeWheel = 17,
 
356
        StopFreeWheel = 18,
 
357
        IntClientHandle = 19,
 
358
        IntClientLoad = 20,
 
359
        IntClientName = 21,
 
360
        IntClientUnload = 22,
 
361
        RecomputeTotalLatencies = 23,
 
362
        RecomputeTotalLatency = 24
 
363
} RequestType;
 
364
 
 
365
struct _jack_request {
 
366
    
 
367
    //RequestType type;
 
368
    uint32_t type;
 
369
    union {
 
370
        struct {
 
371
            char name[JACK_PORT_NAME_SIZE];
 
372
            char type[JACK_PORT_TYPE_SIZE];
 
373
            uint32_t         flags;
 
374
            jack_shmsize_t   buffer_size;
 
375
            jack_port_id_t   port_id;
 
376
            jack_client_id_t client_id;
 
377
        } POST_PACKED_STRUCTURE port_info;
 
378
        struct {
 
379
            char source_port[JACK_PORT_NAME_SIZE];
 
380
            char destination_port[JACK_PORT_NAME_SIZE];
 
381
        } POST_PACKED_STRUCTURE connect;
 
382
        struct {
 
383
            int32_t nports;
 
384
            const char **ports; /* this is only exposed to internal clients, so there
 
385
                                   is no 64/32 issue. external clients read the ports
 
386
                                   one by one from the server, and allocate their
 
387
                                   own "ports" array in their own address space.
 
388
                                */
 
389
        } POST_PACKED_STRUCTURE port_connections;
 
390
        struct {
 
391
            jack_client_id_t client_id;
 
392
            int32_t conditional;
 
393
        } POST_PACKED_STRUCTURE timebase;
 
394
        struct {
 
395
            //jack_options_t options;
 
396
            uint32_t options;
 
397
            jack_client_id_t id;
 
398
            char name[JACK_CLIENT_NAME_SIZE];
 
399
            char path[PATH_MAX+1];
 
400
            char init[JACK_LOAD_INIT_LIMIT];
 
401
        } POST_PACKED_STRUCTURE intclient;
 
402
        jack_client_id_t client_id;
 
403
        jack_nframes_t nframes;
 
404
        jack_time_t timeout;
 
405
        pid_t cap_pid;
 
406
    } POST_PACKED_STRUCTURE x;
 
407
    int32_t status;
 
408
} POST_PACKED_STRUCTURE;
 
409
 
 
410
/* Per-client structure allocated in the server's address space.
 
411
 * It's here because its not part of the engine structure.
 
412
 */
 
413
 
 
414
typedef struct _jack_client_internal {
 
415
 
 
416
    jack_client_control_t *control;
 
417
 
 
418
    int        request_fd;
 
419
    int        event_fd;
 
420
    int        subgraph_start_fd;
 
421
    int        subgraph_wait_fd;
 
422
    JSList    *ports;    /* protected by engine->client_lock */
 
423
    JSList    *truefeeds;    /* protected by engine->client_lock */
 
424
    JSList    *sortfeeds;    /* protected by engine->client_lock */
 
425
    int        fedcount;
 
426
    int        tfedcount;
 
427
    jack_shm_info_t control_shm;
 
428
    unsigned long execution_order;
 
429
    struct  _jack_client_internal *next_client; /* not a linked list! */
 
430
    dlhandle handle;
 
431
    int     (*initialize)(jack_client_t*, const char*); /* int. clients only */
 
432
    void    (*finish)(void *);          /* internal clients only */
 
433
    int      error;
 
434
    
 
435
#ifdef JACK_USE_MACH_THREADS
 
436
    /* specific resources for server/client real-time thread communication */
 
437
    mach_port_t serverport;
 
438
    trivial_message message;
 
439
    int running;
 
440
    int portnum;
 
441
#endif /* JACK_USE_MACH_THREADS */
 
442
   
 
443
#if 0
 
444
    /* callbacks 
 
445
     */
 
446
    JackProcessCallback process;
 
447
    void *process_arg;
 
448
    JackThreadInitCallback thread_init;
 
449
    void *thread_init_arg;
 
450
    JackBufferSizeCallback bufsize;
 
451
    void *bufsize_arg;
 
452
    JackSampleRateCallback srate;
 
453
    void *srate_arg;
 
454
    JackPortRegistrationCallback port_register;
 
455
    void *port_register_arg;
 
456
    JackPortConnectCallback port_connect;
 
457
    void *port_connect_arg;
 
458
    JackGraphOrderCallback graph_order;
 
459
    void *graph_order_arg;
 
460
    JackXRunCallback xrun;
 
461
    void *xrun_arg;
 
462
    JackSyncCallback sync_cb;
 
463
    void *sync_arg;
 
464
    JackTimebaseCallback timebase_cb;
 
465
    void *timebase_arg;
 
466
    JackFreewheelCallback freewheel_cb;
 
467
    void *freewheel_arg;
 
468
    JackClientRegistrationCallback client_register;     
 
469
    void *client_register_arg;
 
470
        JackThreadCallback thread_cb;   
 
471
    void *thread_cb_arg;
 
472
#endif
 
473
    /* external clients: set by libjack
 
474
     * internal clients: set by engine */
 
475
    //int (*deliver_request)(void*, jack_request_t*); /* JOQ: 64/32 bug! */
 
476
    //void *deliver_arg;
 
477
    jack_client_t *private_client;
 
478
} jack_client_internal_t;
 
479
 
 
480
typedef struct _jack_thread_arg {
 
481
        jack_client_t* client;
 
482
        void* (*work_function)(void*);
 
483
        int priority;
 
484
        int realtime;
 
485
        void* arg;
 
486
        pid_t cap_pid;
 
487
} jack_thread_arg_t;
 
488
 
 
489
extern int  jack_client_handle_port_connection (jack_client_t *client,
 
490
                                                jack_event_t *event);
 
491
extern jack_client_t *jack_driver_client_new (jack_engine_t *,
 
492
                                              const char *client_name);
 
493
extern jack_client_t *jack_client_alloc_internal (jack_client_control_t*,
 
494
                                                  jack_engine_t*);
 
495
 
 
496
/* internal clients call this. it's defined in jack/engine.c */
 
497
void handle_internal_client_request (jack_control_t*, jack_request_t*);
 
498
 
 
499
extern char *jack_tmpdir;
 
500
 
 
501
extern char *jack_user_dir (void);
 
502
 
 
503
extern char *jack_server_dir (const char *server_name, char *server_dir);
 
504
 
 
505
extern void *jack_zero_filled_buffer;
 
506
 
 
507
extern jack_port_functions_t jack_builtin_audio_functions;
 
508
 
 
509
extern jack_port_type_info_t jack_builtin_port_types[];
 
510
 
 
511
extern void jack_client_invalidate_port_buffers (jack_client_t *client);
 
512
 
 
513
extern void jack_transport_copy_position (jack_position_t *from,
 
514
                                          jack_position_t *to);
 
515
extern void jack_call_sync_client (jack_client_t *client);
 
516
 
 
517
extern void jack_call_timebase_master (jack_client_t *client);
 
518
 
 
519
extern char *jack_default_server_name (void);
 
520
 
 
521
void silent_jack_error_callback (const char *desc);
 
522
 
 
523
/* needed for port management */
 
524
jack_port_t *jack_port_by_id_int (const jack_client_t *client,
 
525
                                  jack_port_id_t id, int* free);
 
526
 
 
527
jack_port_t *jack_port_by_name_int (jack_client_t *client,
 
528
                                    const char *port_name);
 
529
int jack_port_name_equals (jack_port_shared_t* port, const char* target);
 
530
 
 
531
#ifdef __GNUC__
 
532
#  define likely(x)     __builtin_expect((x),1)
 
533
#  define unlikely(x)   __builtin_expect((x),0)
 
534
#else
 
535
#  define likely(x)     (x)
 
536
#  define unlikely(x)   (x)
 
537
#endif
 
538
 
 
539
#endif /* __jack_internal_h__ */
 
540