917fea8930cd3a6010f98c68117193ed93aaee59
[melted] / src / modules / jackrack / filter_jackrack.c
1 /*
2 * filter_jackrack.c -- filter audio through Jack and/or LADSPA plugins
3 * Copyright (C) 2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "filter_jackrack.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <jack/jack.h>
31 #include <jack/ringbuffer.h>
32 #include <pthread.h>
33 #include <string.h>
34
35 #include "ui.h"
36
37 #define BUFFER_LEN 2048 * 20
38
39 static void *jackrack_thread( void *arg )
40 {
41 mlt_properties properties = arg;
42 ui_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
43
44 while ( mlt_properties_get_int( properties, "_done" ) == 0 )
45 if ( ui_loop_iterate( jackrack ) )
46 break;
47
48 ui_quit( jackrack );
49 ui_destroy( jackrack );
50
51 return NULL;
52 }
53
54 static void initialise_jack_ports( mlt_properties properties, int channels, int samples )
55 {
56 int i;
57 char mlt_name[20], rack_name[30];
58 jack_port_t **port = NULL;
59 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
60 jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
61
62 // Propogate these for the Jack processing callback
63 mlt_properties_set_int( properties, "_channels", channels );
64 mlt_properties_set_int( properties, "_samples", samples );
65
66 // Start JackRack
67 if ( mlt_properties_get( properties, "src" ) )
68 {
69 pthread_t *jackrack_pthread = mlt_pool_alloc( sizeof( pthread_t ) );
70
71 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
72 ui_t *jackrack = ui_new( rack_name, mlt_properties_get_int( properties, "_channels" ), 0, 0 );
73 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
74
75 mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
76 mlt_properties_set( properties, "_rack_client_name", rack_name );
77 mlt_properties_set_int( properties, "_done", 0 );
78 mlt_properties_set_data( properties, "jackrack_pthread", jackrack_pthread, 0, NULL, NULL );
79
80 pthread_create( jackrack_pthread, NULL, jackrack_thread, properties );
81 }
82
83 // Allocate buffers and ports
84 jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
85 jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
86 jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
87 jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
88 float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
89 float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
90
91 // Set properties for self-destruction
92 mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
93 mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
94 mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
95 mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
96 mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, NULL, NULL );
97 mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, NULL, NULL );
98
99 // Start Jack processing - required before registering ports
100 jack_activate( jack_client );
101
102 // Register Jack ports
103 for ( i = 0; i < channels; i++ )
104 {
105 int in;
106
107 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
108 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
109 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
110 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
111 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
112 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
113
114 for ( in = 0; in < 2; in++ )
115 {
116 snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
117 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
118
119 *port = jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
120 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
121 }
122 }
123
124 // Establish connections
125 for ( i = 0; i < channels; i++ )
126 {
127 int in;
128 for ( in = 0; in < 2; in++ )
129 {
130 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
131 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
132
133 snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
134 if ( mlt_properties_get( properties, "_rack_client_name" ) )
135 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
136 else if ( mlt_properties_get( properties, rack_name ) )
137 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
138 else
139 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
140
141 if ( in )
142 {
143 fprintf( stderr, "jack connect %s to %s\n", rack_name, mlt_name );
144 jack_connect( jack_client, rack_name, mlt_name );
145 }
146 else
147 {
148 fprintf( stderr, "jack connect %s to %s\n", mlt_name, rack_name );
149 jack_connect( jack_client, mlt_name, rack_name );
150 }
151 }
152 }
153 }
154
155 static int jack_process (jack_nframes_t frames, void * data)
156 {
157 mlt_filter filter = (mlt_filter) data;
158 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
159 int channels = mlt_properties_get_int( properties, "_channels" );
160 int err = 0;
161 int i;
162 static size_t total_size = 0;
163 size_t first_ring_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
164
165 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
166 if ( output_buffers == NULL )
167 return 0;
168 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
169 jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
170 jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
171 float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
172 float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
173 pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
174 pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
175
176 for ( i = 0; i < channels; i++ )
177 {
178 size_t jack_size = ( frames * sizeof(float) );
179 size_t ring_size;
180
181 // Send audio through out port
182 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
183 if ( ! jack_output_buffers[i] )
184 {
185 fprintf( stderr, "%s: no jack buffer for output port %d\n", __FUNCTION__, i );
186 err = 1;
187 break;
188 }
189 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
190 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
191
192 // Do not start returning audio until we have sent first mlt frame
193 if ( first_ring_size != -sizeof(float) && i == 0 )
194 total_size += ring_size;
195 if ( first_ring_size == -sizeof(float) || total_size >= first_ring_size )
196 {
197 // Return audio through in port
198 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
199 if ( ! jack_input_buffers[i] )
200 {
201 fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
202 err = 1;
203 break;
204 }
205
206 ring_size = jack_ringbuffer_write_space( input_buffers[i] );
207 jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
208
209 // Tell mlt that audio is available
210 if ( first_ring_size != -sizeof(float) && i == ( channels - 1 )
211 && pthread_mutex_trylock( output_lock) == 0 )
212 {
213 pthread_cond_signal( output_ready );
214 pthread_mutex_unlock( output_lock );
215 }
216
217 // Set flag to skip this henceforth
218 mlt_properties_set_int( properties, "_samples", -1 );
219 }
220 }
221
222 return err;
223 }
224
225
226 /** Get the audio.
227 */
228
229 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
230 {
231 // Get the filter service
232 mlt_filter filter = mlt_frame_pop_audio( frame );
233
234 // Get the filter properties
235 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
236
237 // Restore the original get_audio
238 frame->get_audio = mlt_frame_pop_audio( frame );
239
240 int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
241
242 // Get the producer's audio
243 mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
244 //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
245
246 // Deal with sample rate differences
247 if ( *frequency != jack_frequency )
248 fprintf( stderr, "mismatching frequencies in filter jackrack\n" );
249 *frequency = jack_frequency;
250
251 // Initialise Jack ports and connections if needed
252 if ( ! mlt_properties_get_data( filter_properties, "jack_output_ports", NULL ) )
253 initialise_jack_ports( filter_properties, *channels, *samples );
254
255 // Get the filter-specific properties
256 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
257 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
258 pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
259 pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
260
261 // Process the audio
262 int16_t *q = *buffer;
263 float sample;
264 int i, j;
265
266 // Convert to floats and write into output ringbuffer
267 if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
268 {
269 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
270 //pthread_cond_wait( &output_ready, &output_lock );
271 for ( i = 0; i < *samples; i++ )
272 for ( j = 0; j < *channels; j++ )
273 {
274 sample = ( float )( *q ++ ) / 32768.0;
275 jack_ringbuffer_write( output_buffers[j], ( char * )&sample, sizeof(float) );
276 }
277 }
278 //else
279 // fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
280
281 // Read from input ringbuffer and convert from floats
282 while ( mlt_properties_get_int( filter_properties, "_samples" ) != -1
283 && jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) )
284 pthread_cond_wait( output_ready, output_lock );
285
286 // Initialise to silence, but repeat last frame if available in case of
287 // buffer underrun
288 sample = 0;
289 q = *buffer;
290 for ( i = 0; i < *samples; i++ )
291 for ( j = 0; j < *channels; j++ )
292 {
293 jack_ringbuffer_read( input_buffers[j], ( char * )&sample, sizeof(float) );
294
295 if ( sample > 1.0 )
296 sample = 1.0;
297 else if ( sample < -1.0 )
298 sample = -1.0;
299
300 if ( sample > 0 )
301 *q ++ = 32767 * sample;
302 else
303 *q ++ = 32768 * sample;
304 }
305
306 return 0;
307 }
308
309
310 /** Filter processing.
311 */
312
313 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
314 {
315 if ( frame->get_audio != NULL )
316 {
317 mlt_frame_push_audio( frame, frame->get_audio );
318 mlt_frame_push_audio( frame, this );
319 frame->get_audio = jackrack_get_audio;
320 }
321
322 return frame;
323 }
324
325
326 void filter_close( mlt_filter this )
327 {
328 int i;
329 char mlt_name[20];
330 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
331 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
332
333 jack_deactivate( jack_client );
334 jack_client_close( jack_client );
335 for ( i = 0; i < mlt_properties_get_int( properties, "_channels" ); i++ )
336 {
337 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
338 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
339 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
340 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
341 }
342 mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
343 mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
344 mlt_pool_release( mlt_properties_get_data( properties, "jack_output_ports", NULL ) );
345 mlt_pool_release( mlt_properties_get_data( properties, "jack_input_ports", NULL ) );
346 mlt_pool_release( mlt_properties_get_data( properties, "jack_output_buffers", NULL ) );
347 mlt_pool_release( mlt_properties_get_data( properties, "jack_input_buffers", NULL ) );
348 mlt_pool_release( mlt_properties_get_data( properties, "output_lock", NULL ) );
349 mlt_pool_release( mlt_properties_get_data( properties, "output_ready", NULL ) );
350
351 pthread_t *jackrack_pthread = mlt_properties_get_data( properties, "jackrack_thread", NULL );
352 if ( jackrack_pthread != NULL )
353 {
354 mlt_properties_set_int( properties, "_done", 1 );
355 pthread_join( *jackrack_pthread, NULL );
356 mlt_pool_release( jackrack_pthread );
357 }
358 }
359
360 /** Constructor for the filter.
361 */
362
363 mlt_filter filter_jackrack_init( char *arg )
364 {
365 mlt_filter this = mlt_filter_new( );
366 if ( this != NULL )
367 {
368 char name[14];
369
370 snprintf( name, sizeof( name ), "mlt%d", getpid() );
371 jack_client_t *jack_client = jack_client_new( name );
372 if ( jack_client )
373 {
374 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
375 pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
376 pthread_cond_t *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
377
378 jack_set_process_callback( jack_client, jack_process, this );
379 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
380 this->process = filter_process;
381 pthread_mutex_init( output_lock, NULL );
382 pthread_cond_init( output_ready, NULL );
383
384 mlt_properties_set( properties, "src", arg );
385 mlt_properties_set( properties, "_client_name", name );
386 mlt_properties_set_data( properties, "jack_client", jack_client, 0, ( mlt_destructor )filter_close, NULL );
387 mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
388 mlt_properties_set_data( properties, "output_lock", output_lock, 0, NULL, NULL );
389 mlt_properties_set_data( properties, "output_ready", output_ready, 0, NULL, NULL );
390 }
391 }
392 return this;
393 }