summaryrefslogtreecommitdiff
path: root/drivers/freebob/freebob_driver.h
blob: 4f638f02a2567265e72906656073e99a5e9e23e5 (plain)
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
/* freebob_driver.h
 *
 *   FreeBob Backend for Jack
 *   FreeBob = Firewire (pro-)audio for linux
 *
 *   http://freebob.sf.net
 *   http://jackit.sf.net
 *
 *   Copyright (C) 2005-2007 Pieter Palmers <pieter.palmers@ffado.org>
 *
 *   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.
 */

/*
 * Main Jack driver entry routines
 *
 */

#ifndef __JACK_FREEBOB_DRIVER_H__
#define __JACK_FREEBOB_DRIVER_H__

// Only one of these !
#define FREEBOB_DRIVER_WITH_ALSA_MIDI
//#define FREEBOB_DRIVER_WITH_JACK_MIDI

#ifdef FREEBOB_DRIVER_WITH_ALSA_MIDI
	#ifdef FREEBOB_DRIVER_WITH_JACK_MIDI
		#error "Can't have both ALSA midi and JACK midi defined for the FreeBoB backend"
	#endif
#endif

#ifdef FREEBOB_DRIVER_WITH_JACK_MIDI
	#warning "JACK midi for FreeBoB is experimental and has severe jitter issues."
#endif

#include <libfreebob/freebob.h>
#include <libfreebob/freebob_streaming.h>

#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <poll.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <endian.h>

#include <pthread.h>
#include <semaphore.h>

#include <jack/types.h>

#include "driver.h"
#include "engine.h"

// debug print control flags
#define DEBUG_LEVEL_BUFFERS             (1 << 0)
#define DEBUG_LEVEL_HANDLERS                    (1 << 1)
#define DEBUG_LEVEL_XRUN_RECOVERY       (1 << 2)
#define DEBUG_LEVEL_WAIT                        (1 << 3)

#define DEBUG_LEVEL_RUN_CYCLE           (1 << 8)

#define DEBUG_LEVEL_PACKETCOUNTER               (1 << 16)
#define DEBUG_LEVEL_STARTUP                             (1 << 17)
#define DEBUG_LEVEL_THREADS                             (1 << 18)


#ifdef DEBUG_ENABLED

// default debug level
	#define DEBUG_LEVEL (  DEBUG_LEVEL_RUN_CYCLE | \
			       (DEBUG_LEVEL_XRUN_RECOVERY) | DEBUG_LEVEL_STARTUP | DEBUG_LEVEL_WAIT | DEBUG_LEVEL_PACKETCOUNTER)

	#warning Building debug build!

	#define printMessage(format, args ...) jack_error ( "FreeBoB MSG: %s:%d (%s): " format,  __FILE__, __LINE__, __FUNCTION__, ## args )
	#define printError(format, args ...) jack_error ( "FreeBoB ERR: %s:%d (%s): " format,  __FILE__, __LINE__, __FUNCTION__, ## args )

//#define printEnter() jack_error( "FBDRV ENTERS: %s (%s)\n", __FUNCTION__,  __FILE__)
//#define printExit() jack_error( "FBDRV EXITS: %s (%s)\n", __FUNCTION__,  __FILE__)
	#define printEnter()
	#define printExit()

	#define debugError(format, args ...) jack_error ( "FREEBOB ERR: %s:%d (%s): " format,  __FILE__, __LINE__, __FUNCTION__, ## args )
	#define debugPrint(Level, format, args ...) if (DEBUG_LEVEL & (Level)) { jack_error ("DEBUG %s:%d (%s) :"  format, __FILE__, __LINE__, __FUNCTION__, ## args ); }
	#define debugPrintShort(Level, format, args ...) if (DEBUG_LEVEL & (Level)) { jack_error ( format, ## args ); }
	#define debugPrintWithTimeStamp(Level, format, args ...) if (DEBUG_LEVEL & (Level)) { jack_error ( "%16lu: "format, debugGetCurrentUTime (), ## args ); }
	#define SEGFAULT int *test = NULL;        *test = 1;
#else
	#define DEBUG_LEVEL

	#define printMessage(format, args ...) if (g_verbose) \
		jack_error ("FreeBoB MSG: " format, ## args )
	#define printError(format, args ...)   jack_error ("FreeBoB ERR: " format, ## args )

	#define printEnter()
	#define printExit()

	#define debugError(format, args ...)
	#define debugPrint(Level, format, args ...)
	#define debugPrintShort(Level, format, args ...)
	#define debugPrintWithTimeStamp(Level, format, args ...)
#endif

// thread priority setup
#define FREEBOB_RT_PRIORITY_PACKETIZER_RELATIVE 5

// MIDI

#ifdef FREEBOB_DRIVER_WITH_ALSA_MIDI

#include <jack/thread.h>
#include <alsa/asoundlib.h>

#define ALSA_SEQ_BUFF_SIZE 1024
#define MIDI_TRANSMIT_BUFFER_SIZE 1024
#define MIDI_THREAD_SLEEP_TIME_USECS 100
// midi priority should be higher than the audio priority in order to
// make sure events are not only delivered on period boundarys
// but I think it should be smaller than the packetizer thread in order not
// to lose any packets
#define FREEBOB_RT_PRIORITY_MIDI_RELATIVE       4

#endif // FREEBOB_DRIVER_WITH_ALSA_MIDI

#ifdef FREEBOB_DRIVER_WITH_JACK_MIDI

#include "../alsa_midi/midi_pack.h"
#include "../alsa_midi/midi_unpack.h"
#include <jack/midiport.h>

typedef struct freebob_midi_input_port_t {
	// jack
	midi_unpack_t unpack;

	// midi
	int overruns;
} freebob_midi_input_port_t;

typedef struct freebob_midi_output_port_t {
	// jack
	midi_pack_t packer;
} freebob_midi_output_port_t;

#endif // FREEBOB_DRIVER_WITH_JACK_MIDI


typedef struct _freebob_driver freebob_driver_t;

/*
 * Jack Driver command line parameters
 */

typedef struct _freebob_jack_settings freebob_jack_settings_t;
struct _freebob_jack_settings {
	int period_size_set;
	jack_nframes_t period_size;

	int sample_rate_set;
	int sample_rate;

	int buffer_size_set;
	jack_nframes_t buffer_size;

	int port_set;
	int port;

	int node_id_set;
	int node_id;

	int playback_ports;
	int capture_ports;

	jack_nframes_t capture_frame_latency;
	jack_nframes_t playback_frame_latency;

	freebob_handle_t fb_handle;
};

#ifdef FREEBOB_DRIVER_WITH_ALSA_MIDI


typedef struct {
	int stream_nr;
	int seq_port_nr;
	snd_midi_event_t *parser;
	snd_seq_t *seq_handle;
} freebob_midi_port_t;

typedef struct _freebob_driver_midi_handle {
	freebob_device_t *dev;
	freebob_driver_t *driver;

	snd_seq_t *seq_handle;

	pthread_t queue_thread;
	pthread_t dequeue_thread;
	int queue_thread_realtime;
	int queue_thread_priority;

	int nb_input_ports;
	int nb_output_ports;

	freebob_midi_port_t **input_ports;
	freebob_midi_port_t **output_ports;

	freebob_midi_port_t **input_stream_port_map;
	int *output_port_stream_map;


} freebob_driver_midi_handle_t;

#endif
/*
 * JACK driver structure
 */


struct _freebob_driver {
	JACK_DRIVER_NT_DECL

	jack_nframes_t sample_rate;
	jack_nframes_t period_size;
	unsigned long wait_time;

	jack_time_t wait_last;
	jack_time_t wait_next;
	int wait_late;

	jack_client_t  *client;

	int xrun_detected;
	int xrun_count;

	int process_count;

	/* settings from the command line */
	freebob_jack_settings_t settings;

	/* the freebob virtual device */
	freebob_device_t *dev;

	JSList                       *capture_ports;
	JSList                       *playback_ports;
	JSList                       *monitor_ports;
	channel_t playback_nchannels;
	channel_t capture_nchannels;

	jack_nframes_t playback_frame_latency;
	jack_nframes_t capture_frame_latency;

	freebob_device_info_t device_info;
	freebob_options_t device_options;

#ifdef FREEBOB_DRIVER_WITH_ALSA_MIDI
	freebob_driver_midi_handle_t *midi_handle;
#endif

#ifdef FREEBOB_DRIVER_WITH_JACK_MIDI
	freebob_midi_input_port_t  *midi_in_ports;
	freebob_midi_output_port_t *midi_out_ports;
#endif


};



#endif /* __JACK_FREEBOB_DRIVER_H__ */