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 <framework/mlt.h>
25 #include <sys/types.h>
29 #include <jack/jack.h>
30 #include <jack/ringbuffer.h>
33 #include "jack_rack.h"
35 extern pthread_mutex_t g_activate_mutex;
37 #define BUFFER_LEN 204800 * 6
39 static void jack_started_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
41 listener( owner, service, (mlt_position*) args[0] );
44 static void jack_stopped_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
46 listener( owner, service, (mlt_position*) args[0] );
49 static void jack_seek_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
51 listener( owner, service, (mlt_position*) args[0] );
54 #define JACKSTATE(x) (x==JackTransportStopped?"stopped":x==JackTransportStarting?"starting":x==JackTransportRolling?"rolling":"unknown")
56 static int jack_sync( jack_transport_state_t state, jack_position_t *jack_pos, void *arg )
58 mlt_filter filter = (mlt_filter) arg;
59 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
60 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE(filter) );
61 mlt_position position = mlt_profile_fps( profile ) * jack_pos->frame / jack_pos->frame_rate + 0.5;
64 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%s frame %u rate %u pos %d last_pos %d\n",
65 JACKSTATE(state), jack_pos->frame, jack_pos->frame_rate, position,
66 mlt_properties_get_position( properties, "_last_pos" ) );
67 if ( state == JackTransportStopped )
69 mlt_events_fire( properties, "jack-stopped", &position, NULL );
70 mlt_properties_set_int( properties, "_sync_guard", 0 );
72 else if ( state == JackTransportStarting )
75 if ( !mlt_properties_get_int( properties, "_sync_guard" ) )
77 mlt_properties_set_int( properties, "_sync_guard", 1 );
78 mlt_events_fire( properties, "jack-started", &position, NULL );
80 else if ( position >= mlt_properties_get_position( properties, "_last_pos" ) - 2 )
82 mlt_properties_set_int( properties, "_sync_guard", 0 );
88 mlt_properties_set_int( properties, "_sync_guard", 0 );
94 static void on_jack_start( mlt_properties owner, mlt_properties properties )
96 mlt_log_verbose( NULL, "%s\n", __FUNCTION__ );
97 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
98 jack_transport_start( jack_client );
101 static void on_jack_stop( mlt_properties owner, mlt_properties properties )
103 mlt_log_verbose( NULL, "%s\n", __FUNCTION__ );
104 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
105 jack_transport_stop( jack_client );
108 static void on_jack_seek( mlt_properties owner, mlt_filter filter, mlt_position *position )
110 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
111 mlt_log_verbose( MLT_FILTER_SERVICE(filter), "%s: %d\n", __FUNCTION__, *position );
112 mlt_properties_set_int( properties, "_sync_guard", 1 );
113 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
114 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
115 jack_nframes_t jack_frame = jack_get_sample_rate( jack_client );
116 jack_frame *= *position / mlt_profile_fps( profile );
117 jack_transport_locate( jack_client, jack_frame );
120 static void initialise_jack_ports( mlt_properties properties )
123 char mlt_name[20], rack_name[30];
124 jack_port_t **port = NULL;
125 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
126 jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
128 // Propogate these for the Jack processing callback
129 int channels = mlt_properties_get_int( properties, "channels" );
132 if ( mlt_properties_get( properties, "src" ) )
134 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
135 jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
136 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
138 mlt_properties_set_data( properties, "jackrack", jackrack, 0,
139 (mlt_destructor) jack_rack_destroy, NULL );
140 mlt_properties_set( properties, "_rack_client_name", rack_name );
144 // We have to set this to something to prevent re-initialization.
145 mlt_properties_set_data( properties, "jackrack", jack_client, 0, NULL, NULL );
148 // Allocate buffers and ports
149 jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
150 jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
151 jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
152 jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
153 float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
154 float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
156 // Set properties - released inside filter_close
157 mlt_properties_set_data( properties, "output_buffers", output_buffers,
158 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
159 mlt_properties_set_data( properties, "input_buffers", input_buffers,
160 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
161 mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports,
162 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
163 mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports,
164 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
165 mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers,
166 sizeof( float *) * channels, mlt_pool_release, NULL );
167 mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers,
168 sizeof( float *) * channels, mlt_pool_release, NULL );
170 // Register Jack ports
171 for ( i = 0; i < channels; i++ )
175 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
176 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
177 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
178 mlt_properties_set_data( properties, mlt_name, output_buffers[i],
179 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
180 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
181 mlt_properties_set_data( properties, mlt_name, input_buffers[i],
182 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
184 for ( in = 0; in < 2; in++ )
186 snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
187 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
189 *port = jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
190 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
194 // Start Jack processing
195 pthread_mutex_lock( &g_activate_mutex );
196 jack_activate( jack_client );
197 pthread_mutex_unlock( &g_activate_mutex );
199 // Establish connections
200 for ( i = 0; i < channels; i++ )
203 for ( in = 0; in < 2; in++ )
205 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
206 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
208 snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
209 if ( mlt_properties_get( properties, "_rack_client_name" ) )
210 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
211 else if ( mlt_properties_get( properties, rack_name ) )
212 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
214 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
218 mlt_log_verbose( NULL, "JACK connect %s to %s\n", rack_name, mlt_name );
219 jack_connect( jack_client, rack_name, mlt_name );
223 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, rack_name );
224 jack_connect( jack_client, mlt_name, rack_name );
230 static int jack_process (jack_nframes_t frames, void * data)
232 mlt_filter filter = (mlt_filter) data;
233 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
234 int channels = mlt_properties_get_int( properties, "channels" );
235 int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
236 int sync = mlt_properties_get_int( properties, "_sync" );
239 static int total_size = 0;
241 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
242 if ( output_buffers == NULL )
244 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
245 jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
246 jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
247 float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
248 float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
249 pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
250 pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
252 for ( i = 0; i < channels; i++ )
254 size_t jack_size = ( frames * sizeof(float) );
257 // Send audio through out port
258 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
259 if ( ! jack_output_buffers[i] )
261 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for output port %d\n", i );
265 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
266 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
267 if ( ring_size < jack_size )
268 memset( &jack_output_buffers[i][ring_size], 0, jack_size - ring_size );
270 // Return audio through in port
271 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
272 if ( ! jack_input_buffers[i] )
274 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for input port %d\n", i );
279 // Do not start returning audio until we have sent first mlt frame
280 if ( sync && i == 0 && frame_size > 0 )
281 total_size += ring_size;
282 mlt_log_debug( MLT_FILTER_SERVICE(filter), "sync %d frame_size %d ring_size %zu jack_size %zu\n", sync, frame_size, ring_size, jack_size );
284 if ( ! sync || ( frame_size > 0 && total_size >= frame_size ) )
286 ring_size = jack_ringbuffer_write_space( input_buffers[i] );
287 jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
291 // Tell mlt that audio is available
292 pthread_mutex_lock( output_lock);
293 pthread_cond_signal( output_ready );
294 pthread_mutex_unlock( output_lock);
297 mlt_properties_set_int( properties, "_sync", 0 );
302 // Often jackd does not send the stopped event through the JackSyncCallback
303 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
304 jack_position_t jack_pos;
305 jack_transport_state_t state = jack_transport_query( jack_client, &jack_pos );
306 int transport_state = mlt_properties_get_int( properties, "_transport_state" );
307 if ( state != transport_state )
309 mlt_properties_set_int( properties, "_transport_state", state );
310 if ( state == JackTransportStopped )
311 jack_sync( state, &jack_pos, filter );
321 static int jackrack_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
323 // Get the filter service
324 mlt_filter filter = mlt_frame_pop_audio( frame );
326 // Get the filter properties
327 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
329 int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
331 // Get the producer's audio
332 *format = mlt_audio_float;
333 mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
335 // TODO: Deal with sample rate differences
336 if ( *frequency != jack_frequency )
337 mlt_log_error( MLT_FILTER_SERVICE( filter ), "mismatching frequencies JACK = %d actual = %d\n",
338 jack_frequency, *frequency );
339 *frequency = jack_frequency;
341 // Initialise Jack ports and connections if needed
342 if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
343 mlt_properties_set_int( filter_properties, "_samples", *samples );
345 // Get the filter-specific properties
346 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
347 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
348 // pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
349 // pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
352 float *q = (float*) *buffer;
353 size_t size = *samples * sizeof(float);
355 // struct timespec tm = { 0, 0 };
357 // Write into output ringbuffer
358 for ( j = 0; j < *channels; j++ )
360 if ( jack_ringbuffer_write_space( output_buffers[j] ) >= size )
361 jack_ringbuffer_write( output_buffers[j], (char*)( q + j * *samples ), size );
364 // Synchronization phase - wait for signal from Jack process
365 while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < size ) ;
366 //pthread_cond_wait( output_ready, output_lock );
368 // Read from input ringbuffer
369 for ( j = 0; j < *channels; j++, q++ )
371 if ( jack_ringbuffer_read_space( input_buffers[j] ) >= size )
372 jack_ringbuffer_read( input_buffers[j], (char*)( q + j * *samples ), size );
375 // help jack_sync() indicate when we are rolling
376 mlt_position pos = mlt_frame_get_position( frame );
377 mlt_properties_set_position( filter_properties, "_last_pos", pos );
383 /** Filter processing.
386 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
389 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
390 mlt_frame_push_audio( frame, this );
391 mlt_frame_push_audio( frame, jackrack_get_audio );
393 if ( !mlt_properties_get_data( properties, "jackrack", NULL ) )
394 initialise_jack_ports( properties );
401 static void filter_close( mlt_filter this )
403 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
404 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
405 jack_deactivate( jack_client );
406 jack_client_close( jack_client );
407 this->parent.close = NULL;
408 mlt_service_close( &this->parent );
411 /** Constructor for the filter.
414 mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
416 mlt_filter this = mlt_filter_new( );
420 char *jack_client_name;
421 jack_status_t status = 0;
423 snprintf( name, sizeof( name ), "mlt%d", getpid() );
424 jack_client_t *jack_client = jack_client_open( name, JackNullOption, &status, NULL );
427 if ( status & JackNameNotUnique )
429 jack_client_name = jack_get_client_name ( jack_client );
430 strcpy( name, jack_client_name );
433 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
434 pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
435 pthread_cond_t *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
437 jack_set_process_callback( jack_client, jack_process, this );
438 jack_set_sync_callback( jack_client, jack_sync, this );
439 jack_set_sync_timeout( jack_client, 5000000 );
440 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
441 this->process = filter_process;
442 this->close = filter_close;
443 pthread_mutex_init( output_lock, NULL );
444 pthread_cond_init( output_ready, NULL );
446 mlt_properties_set( properties, "src", arg );
447 mlt_properties_set( properties, "_client_name", name );
448 mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
449 mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
450 mlt_properties_set_data( properties, "output_lock", output_lock, 0, mlt_pool_release, NULL );
451 mlt_properties_set_data( properties, "output_ready", output_ready, 0, mlt_pool_release, NULL );
452 mlt_properties_set_int( properties, "_sync", 1 );
453 mlt_properties_set_int( properties, "channels", 2 );
455 mlt_events_register( properties, "jack-started", (mlt_transmitter) jack_started_transmitter );
456 mlt_events_register( properties, "jack-stopped", (mlt_transmitter) jack_stopped_transmitter );
457 mlt_events_register( properties, "jack-start", NULL );
458 mlt_events_register( properties, "jack-stop", NULL );
459 mlt_events_register( properties, "jack-seek", (mlt_transmitter) jack_seek_transmitter );
460 mlt_events_listen( properties, properties, "jack-start", (mlt_listener) on_jack_start );
461 mlt_events_listen( properties, properties, "jack-stop", (mlt_listener) on_jack_stop );
462 mlt_events_listen( properties, this, "jack-seek", (mlt_listener) on_jack_seek );
463 mlt_properties_set_position( properties, "_jack_seek", -1 );
467 mlt_log_error( NULL, "Failed to connect to JACK server\n" );
468 mlt_filter_close( this );