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>
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.
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.
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.
21 #include "filter_jackrack.h"
23 #include <framework/mlt_frame.h>
27 #include <sys/types.h>
30 #include <jack/jack.h>
31 #include <jack/ringbuffer.h>
37 #define BUFFER_LEN 2048 * 3
39 static void *jackrack_thread( void *arg
)
41 mlt_properties properties
= arg
;
42 ui_t
*jackrack
= mlt_properties_get_data( properties
, "jackrack", NULL
);
44 while ( mlt_properties_get_int( properties
, "_done" ) == 0 )
45 if ( ui_loop_iterate( jackrack
) )
49 ui_destroy( jackrack
);
54 static void initialise_jack_ports( mlt_properties properties
)
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
);
62 // Propogate these for the Jack processing callback
63 int channels
= mlt_properties_get_int( properties
, "channels" );
66 if ( mlt_properties_get( properties
, "src" ) )
68 pthread_t
*jackrack_pthread
= mlt_pool_alloc( sizeof( pthread_t
) );
70 snprintf( rack_name
, sizeof( rack_name
), "jackrack%d", getpid() );
71 ui_t
*jackrack
= ui_new( rack_name
, mlt_properties_get_int( properties
, "channels" ), 0, 0 );
72 jack_rack_open_file( jackrack
, mlt_properties_get( properties
, "src" ) );
74 mlt_properties_set_data( properties
, "jackrack", jackrack
, 0, NULL
, NULL
);
75 mlt_properties_set( properties
, "_rack_client_name", rack_name
);
76 mlt_properties_set_int( properties
, "_done", 0 );
77 mlt_properties_set_data( properties
, "jackrack_pthread", jackrack_pthread
, 0, NULL
, NULL
);
79 pthread_create( jackrack_pthread
, NULL
, jackrack_thread
, properties
);
82 // Allocate buffers and ports
83 jack_ringbuffer_t
**output_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
84 jack_ringbuffer_t
**input_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
85 jack_port_t
**jack_output_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
86 jack_port_t
**jack_input_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
87 float **jack_output_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
88 float **jack_input_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
90 // Set properties - released inside filter_close
91 mlt_properties_set_data( properties
, "output_buffers", output_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
92 mlt_properties_set_data( properties
, "input_buffers", input_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
93 mlt_properties_set_data( properties
, "jack_output_ports", jack_output_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
94 mlt_properties_set_data( properties
, "jack_input_ports", jack_input_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
95 mlt_properties_set_data( properties
, "jack_output_buffers", jack_output_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
96 mlt_properties_set_data( properties
, "jack_input_buffers", jack_input_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
98 // Start Jack processing - required before registering ports
99 jack_activate( jack_client
);
101 // Register Jack ports
102 for ( i
= 0; i
< channels
; i
++ )
106 output_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
107 input_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
108 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
109 mlt_properties_set_data( properties
, mlt_name
, output_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
110 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
111 mlt_properties_set_data( properties
, mlt_name
, input_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
113 for ( in
= 0; in
< 2; in
++ )
115 snprintf( mlt_name
, sizeof( mlt_name
), "%s_%d", in ?
"in" : "out", i
+ 1);
116 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
118 *port
= jack_port_register( jack_client
, mlt_name
, JACK_DEFAULT_AUDIO_TYPE
,
119 ( in ? JackPortIsInput
: JackPortIsOutput
) | JackPortIsTerminal
, 0 );
123 // Establish connections
124 for ( i
= 0; i
< channels
; i
++ )
127 for ( in
= 0; in
< 2; in
++ )
129 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
130 snprintf( mlt_name
, sizeof( mlt_name
), "%s", jack_port_name( *port
) );
132 snprintf( rack_name
, sizeof( rack_name
), "%s_%d", in ?
"in" : "out", i
+ 1 );
133 if ( mlt_properties_get( properties
, "_rack_client_name" ) )
134 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_rack_client_name" ), in ?
"out" : "in", i
+ 1);
135 else if ( mlt_properties_get( properties
, rack_name
) )
136 snprintf( rack_name
, sizeof( rack_name
), "%s", mlt_properties_get( properties
, rack_name
) );
138 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_client_name" ), in ?
"out" : "in", i
+ 1);
142 fprintf( stderr
, "jack connect %s to %s\n", rack_name
, mlt_name
);
143 jack_connect( jack_client
, rack_name
, mlt_name
);
147 fprintf( stderr
, "jack connect %s to %s\n", mlt_name
, rack_name
);
148 jack_connect( jack_client
, mlt_name
, rack_name
);
154 static int jack_process (jack_nframes_t frames
, void * data
)
156 mlt_filter filter
= (mlt_filter
) data
;
157 mlt_properties properties
= MLT_FILTER_PROPERTIES( filter
);
158 int channels
= mlt_properties_get_int( properties
, "channels" );
159 int frame_size
= mlt_properties_get_int( properties
, "_samples" ) * sizeof(float);
160 int sync
= mlt_properties_get_int( properties
, "_sync" );
163 static int total_size
= 0;
165 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( properties
, "output_buffers", NULL
);
166 if ( output_buffers
== NULL
)
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
);
176 for ( i
= 0; i
< channels
; i
++ )
178 size_t jack_size
= ( frames
* sizeof(float) );
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
] )
185 fprintf( stderr
, "%s: no jack buffer for output port %d\n", __FUNCTION__
, i
);
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
);
192 // Return audio through in port
193 jack_input_buffers
[i
] = jack_port_get_buffer( jack_input_ports
[i
], frames
);
194 if ( ! jack_input_buffers
[i
] )
196 fprintf( stderr
, "%s: no jack buffer for input port %d\n", __FUNCTION__
, i
);
201 // Do not start returning audio until we have sent first mlt frame
202 if ( sync
&& i
== 0 && frame_size
> 0 )
203 total_size
+= ring_size
;
204 //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
206 if ( ! sync
|| ( frame_size
> 0 && total_size
>= frame_size
) )
208 ring_size
= jack_ringbuffer_write_space( input_buffers
[i
] );
209 jack_ringbuffer_write( input_buffers
[i
], ( char * )jack_input_buffers
[i
], ring_size
< jack_size ? ring_size
: jack_size
);
213 // Tell mlt that audio is available
214 pthread_mutex_lock( output_lock
);
215 pthread_cond_signal( output_ready
);
216 pthread_mutex_unlock( output_lock
);
219 mlt_properties_set_int( properties
, "_sync", 0 );
231 static int jackrack_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
233 // Get the filter service
234 mlt_filter filter
= mlt_frame_pop_audio( frame
);
236 // Get the filter properties
237 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
239 int jack_frequency
= mlt_properties_get_int( filter_properties
, "_sample_rate" );
241 // Get the producer's audio
242 mlt_frame_get_audio( frame
, buffer
, format
, &jack_frequency
, channels
, samples
);
244 // TODO: Deal with sample rate differences
245 if ( *frequency
!= jack_frequency
)
246 fprintf( stderr
, "mismatching frequencies in filter jackrack\n" );
247 *frequency
= jack_frequency
;
249 // Initialise Jack ports and connections if needed
250 if ( mlt_properties_get_int( filter_properties
, "_samples" ) == 0 )
251 mlt_properties_set_int( filter_properties
, "_samples", *samples
);
253 // Get the filter-specific properties
254 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
255 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
256 pthread_mutex_t
*output_lock
= mlt_properties_get_data( filter_properties
, "output_lock", NULL
);
257 pthread_cond_t
*output_ready
= mlt_properties_get_data( filter_properties
, "output_ready", NULL
);
260 int16_t *q
= *buffer
;
264 // Convert to floats and write into output ringbuffer
265 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
267 for ( i
= 0; i
< *samples
; i
++ )
268 for ( j
= 0; j
< *channels
; j
++ )
270 sample
= ( float )( *q
++ ) / 32768.0;
271 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
275 // Synchronization phase - wait for signal from Jack process
276 while ( mlt_properties_get_int( filter_properties
, "_sync" )
277 && jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) )
278 pthread_cond_wait( output_ready
, output_lock
);
280 // Read from input ringbuffer and convert from floats
281 //if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
283 // Initialise to silence, but repeat last frame if available in case of
287 for ( i
= 0; i
< *samples
; i
++ )
288 for ( j
= 0; j
< *channels
; j
++ )
290 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
294 else if ( sample
< -1.0 )
298 *q
++ = 32767 * sample
;
300 *q
++ = 32768 * sample
;
308 /** Filter processing.
311 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
313 if ( mlt_frame_is_test_audio( frame
) != 0 )
315 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
316 mlt_frame_push_audio( frame
, this );
317 mlt_frame_push_audio( frame
, jackrack_get_audio
);
319 if ( mlt_properties_get_int( properties
, "_sync" ) )
320 initialise_jack_ports( properties
);
327 void filter_close( mlt_filter
this )
331 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
332 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
334 jack_deactivate( jack_client
);
335 jack_client_close( jack_client
);
336 for ( i
= 0; i
< mlt_properties_get_int( properties
, "channels" ); i
++ )
338 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
339 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
340 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
341 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
343 mlt_pool_release( mlt_properties_get_data( properties
, "output_buffers", NULL
) );
344 mlt_pool_release( mlt_properties_get_data( properties
, "input_buffers", NULL
) );
345 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_ports", NULL
) );
346 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_ports", NULL
) );
347 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_buffers", NULL
) );
348 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_buffers", NULL
) );
349 mlt_pool_release( mlt_properties_get_data( properties
, "output_lock", NULL
) );
350 mlt_pool_release( mlt_properties_get_data( properties
, "output_ready", NULL
) );
352 pthread_t
*jackrack_pthread
= mlt_properties_get_data( properties
, "jackrack_thread", NULL
);
353 if ( jackrack_pthread
!= NULL
)
355 mlt_properties_set_int( properties
, "_done", 1 );
356 pthread_join( *jackrack_pthread
, NULL
);
357 mlt_pool_release( jackrack_pthread
);
360 this->parent
.close
= NULL
;
361 mlt_service_close( &this->parent
);
364 /** Constructor for the filter.
367 mlt_filter
filter_jackrack_init( char *arg
)
369 mlt_filter
this = mlt_filter_new( );
374 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
375 jack_client_t
*jack_client
= jack_client_new( name
);
378 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
379 pthread_mutex_t
*output_lock
= mlt_pool_alloc( sizeof( pthread_mutex_t
) );
380 pthread_cond_t
*output_ready
= mlt_pool_alloc( sizeof( pthread_cond_t
) );
382 jack_set_process_callback( jack_client
, jack_process
, this );
383 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
384 this->process
= filter_process
;
385 this->close
= filter_close
;
386 pthread_mutex_init( output_lock
, NULL
);
387 pthread_cond_init( output_ready
, NULL
);
389 mlt_properties_set( properties
, "src", arg
);
390 mlt_properties_set( properties
, "_client_name", name
);
391 mlt_properties_set_data( properties
, "jack_client", jack_client
, 0, NULL
, NULL
);
392 mlt_properties_set_int( properties
, "_sample_rate", jack_get_sample_rate( jack_client
) );
393 mlt_properties_set_data( properties
, "output_lock", output_lock
, 0, NULL
, NULL
);
394 mlt_properties_set_data( properties
, "output_ready", output_ready
, 0, NULL
, NULL
);
395 mlt_properties_set_int( properties
, "_sync", 1 );
396 mlt_properties_set_int( properties
, "channels", 2 );