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 #define BUFFER_LEN 204800 * 6
37 static void jack_started_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
39 listener( owner, service, (mlt_position*) args[0] );
42 static void jack_stopped_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
44 listener( owner, service, (mlt_position*) args[0] );
47 static void jack_seek_transmitter( mlt_listener listener, mlt_properties owner, mlt_service service, void **args )
49 listener( owner, service, (mlt_position*) args[0] );
52 #define JACKSTATE(x) (x==JackTransportStopped?"stopped":x==JackTransportStarting?"starting":x==JackTransportRolling?"rolling":"unknown")
54 static int jack_sync( jack_transport_state_t state, jack_position_t *jack_pos, void *arg )
56 mlt_filter filter = (mlt_filter) arg;
57 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
58 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE(filter) );
59 mlt_position position = mlt_profile_fps( profile ) * jack_pos->frame / jack_pos->frame_rate + 0.5;
62 mlt_log_debug( MLT_FILTER_SERVICE(filter), "%s frame %u rate %u pos %d last_pos %d\n",
63 JACKSTATE(state), jack_pos->frame, jack_pos->frame_rate, position,
64 mlt_properties_get_position( properties, "_last_pos" ) );
65 if ( state == JackTransportStopped )
67 mlt_events_fire( properties, "jack-stopped", &position, NULL );
68 mlt_properties_set_int( properties, "_sync_guard", 0 );
70 else if ( state == JackTransportStarting )
73 if ( !mlt_properties_get_int( properties, "_sync_guard" ) )
75 mlt_properties_set_int( properties, "_sync_guard", 1 );
76 mlt_events_fire( properties, "jack-started", &position, NULL );
78 else if ( position >= mlt_properties_get_position( properties, "_last_pos" ) - 2 )
80 mlt_properties_set_int( properties, "_sync_guard", 0 );
86 mlt_properties_set_int( properties, "_sync_guard", 0 );
92 static void on_jack_start( mlt_properties owner, mlt_properties properties )
94 fprintf(stderr, "%s\n", __FUNCTION__);
95 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
96 jack_transport_start( jack_client );
99 static void on_jack_stop( mlt_properties owner, mlt_properties properties )
101 fprintf(stderr, "%s\n", __FUNCTION__);
102 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
103 jack_transport_stop( jack_client );
106 static void on_jack_seek( mlt_properties owner, mlt_filter filter, mlt_position *position )
108 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
111 mlt_properties_set_int( properties, "_sync_guard", 1 );
112 mlt_properties_set_position( properties, "_jack_seek", *position );
116 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
117 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
118 jack_nframes_t jack_frame = jack_get_sample_rate( jack_client );
119 jack_frame *= *position / mlt_profile_fps( profile );
121 fprintf(stderr, "%s: %d\n", __FUNCTION__, *position);
122 jack_transport_locate( jack_client, jack_frame );
125 static void initialise_jack_ports( mlt_properties properties )
128 char mlt_name[20], rack_name[30];
129 jack_port_t **port = NULL;
130 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
131 jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
133 // Propogate these for the Jack processing callback
134 int channels = mlt_properties_get_int( properties, "channels" );
137 if ( mlt_properties_get( properties, "src" ) )
139 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
140 jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
141 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
143 mlt_properties_set_data( properties, "jackrack", jackrack, 0,
144 (mlt_destructor) jack_rack_destroy, NULL );
145 mlt_properties_set( properties, "_rack_client_name", rack_name );
149 // We have to set this to something to prevent re-initialization.
150 mlt_properties_set_data( properties, "jackrack", jack_client, 0, NULL, NULL );
153 // Allocate buffers and ports
154 jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
155 jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
156 jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
157 jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
158 float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
159 float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
161 // Set properties - released inside filter_close
162 mlt_properties_set_data( properties, "output_buffers", output_buffers,
163 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
164 mlt_properties_set_data( properties, "input_buffers", input_buffers,
165 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
166 mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports,
167 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
168 mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports,
169 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
170 mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers,
171 sizeof( float *) * channels, mlt_pool_release, NULL );
172 mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers,
173 sizeof( float *) * channels, mlt_pool_release, NULL );
175 // Start Jack processing - required before registering ports
176 jack_activate( jack_client );
178 // Register Jack ports
179 for ( i = 0; i < channels; i++ )
183 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
184 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
185 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
186 mlt_properties_set_data( properties, mlt_name, output_buffers[i],
187 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
188 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
189 mlt_properties_set_data( properties, mlt_name, input_buffers[i],
190 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
192 for ( in = 0; in < 2; in++ )
194 snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
195 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
197 *port = jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
198 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
202 // Establish connections
203 for ( i = 0; i < channels; i++ )
206 for ( in = 0; in < 2; in++ )
208 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
209 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
211 snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
212 if ( mlt_properties_get( properties, "_rack_client_name" ) )
213 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
214 else if ( mlt_properties_get( properties, rack_name ) )
215 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
217 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
221 mlt_log_verbose( NULL, "JACK connect %s to %s\n", rack_name, mlt_name );
222 jack_connect( jack_client, rack_name, mlt_name );
226 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, rack_name );
227 jack_connect( jack_client, mlt_name, rack_name );
233 static int jack_process (jack_nframes_t frames, void * data)
235 mlt_filter filter = (mlt_filter) data;
236 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
237 int channels = mlt_properties_get_int( properties, "channels" );
238 int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
239 int sync = mlt_properties_get_int( properties, "_sync" );
242 static int total_size = 0;
244 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
245 if ( output_buffers == NULL )
247 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
248 jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
249 jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
250 float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
251 float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
252 pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
253 pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
255 for ( i = 0; i < channels; i++ )
257 size_t jack_size = ( frames * sizeof(float) );
260 // Send audio through out port
261 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
262 if ( ! jack_output_buffers[i] )
264 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for output port %d\n", i );
268 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
269 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
271 // Return audio through in port
272 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
273 if ( ! jack_input_buffers[i] )
275 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for input port %d\n", i );
280 // Do not start returning audio until we have sent first mlt frame
281 if ( sync && i == 0 && frame_size > 0 )
282 total_size += ring_size;
283 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 );
285 if ( ! sync || ( frame_size > 0 && total_size >= frame_size ) )
287 ring_size = jack_ringbuffer_write_space( input_buffers[i] );
288 jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
292 // Tell mlt that audio is available
293 pthread_mutex_lock( output_lock);
294 pthread_cond_signal( output_ready );
295 pthread_mutex_unlock( output_lock);
298 mlt_properties_set_int( properties, "_sync", 0 );
310 static int jackrack_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
312 // Get the filter service
313 mlt_filter filter = mlt_frame_pop_audio( frame );
315 // Get the filter properties
316 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
318 int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
320 // Get the producer's audio
321 *format = mlt_audio_float;
322 mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
324 // TODO: Deal with sample rate differences
325 if ( *frequency != jack_frequency )
326 mlt_log_error( MLT_FILTER_SERVICE( filter ), "mismatching frequencies JACK = %d actual = %d\n",
327 jack_frequency, *frequency );
328 *frequency = jack_frequency;
330 // Initialise Jack ports and connections if needed
331 if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
332 mlt_properties_set_int( filter_properties, "_samples", *samples );
334 // Get the filter-specific properties
335 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
336 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
337 // pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
338 // pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
341 float *q = (float*) *buffer;
342 size_t size = *samples * sizeof(float);
344 // struct timespec tm = { 0, 0 };
346 // Write into output ringbuffer
347 for ( j = 0; j < *channels; j++ )
349 if ( jack_ringbuffer_write_space( output_buffers[j] ) >= size )
350 jack_ringbuffer_write( output_buffers[j], (char*)( q + j * *samples ), size );
353 // Synchronization phase - wait for signal from Jack process
354 while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < size ) ;
355 //pthread_cond_wait( output_ready, output_lock );
357 // Read from input ringbuffer
358 for ( j = 0; j < *channels; j++, q++ )
360 if ( jack_ringbuffer_read_space( input_buffers[j] ) >= size )
361 jack_ringbuffer_read( input_buffers[j], (char*)( q + j * *samples ), size );
364 // Do JACK seeking if requested
365 mlt_position pos = mlt_frame_get_position( frame );
366 mlt_properties_set_position( filter_properties, "_last_pos", pos );
367 if ( pos == mlt_properties_get_position( filter_properties, "_jack_seek" ) )
369 jack_client_t *jack_client = mlt_properties_get_data( filter_properties, "jack_client", NULL );
370 jack_position_t jack_pos;
371 jack_transport_query( jack_client, &jack_pos );
372 double fps = mlt_profile_fps( mlt_service_profile( MLT_FILTER_SERVICE(filter) ) );
373 jack_nframes_t jack_frame = jack_pos.frame_rate * pos / fps;
374 jack_transport_locate( jack_client, jack_frame );
375 mlt_properties_set_position( filter_properties, "_jack_seek", -1 );
382 /** Filter processing.
385 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
388 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
389 mlt_frame_push_audio( frame, this );
390 mlt_frame_push_audio( frame, jackrack_get_audio );
392 if ( !mlt_properties_get_data( properties, "jackrack", NULL ) )
393 initialise_jack_ports( properties );
400 static void filter_close( mlt_filter this )
402 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
403 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
404 jack_deactivate( jack_client );
405 jack_client_close( jack_client );
406 this->parent.close = NULL;
407 mlt_service_close( &this->parent );
410 /** Constructor for the filter.
413 mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
415 mlt_filter this = mlt_filter_new( );
420 snprintf( name, sizeof( name ), "mlt%d", getpid() );
421 jack_client_t *jack_client = jack_client_open( name, JackNullOption, NULL );
424 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
425 pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
426 pthread_cond_t *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
428 jack_set_process_callback( jack_client, jack_process, this );
429 jack_set_sync_callback( jack_client, jack_sync, this );
430 jack_set_sync_timeout( jack_client, 5000000 );
431 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
432 this->process = filter_process;
433 this->close = filter_close;
434 pthread_mutex_init( output_lock, NULL );
435 pthread_cond_init( output_ready, NULL );
437 mlt_properties_set( properties, "src", arg );
438 mlt_properties_set( properties, "_client_name", name );
439 mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
440 mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
441 mlt_properties_set_data( properties, "output_lock", output_lock, 0, mlt_pool_release, NULL );
442 mlt_properties_set_data( properties, "output_ready", output_ready, 0, mlt_pool_release, NULL );
443 mlt_properties_set_int( properties, "_sync", 1 );
444 mlt_properties_set_int( properties, "channels", 2 );
446 mlt_events_register( properties, "jack-started", (mlt_transmitter) jack_started_transmitter );
447 mlt_events_register( properties, "jack-stopped", (mlt_transmitter) jack_stopped_transmitter );
448 mlt_events_register( properties, "jack-start", NULL );
449 mlt_events_register( properties, "jack-stop", NULL );
450 mlt_events_register( properties, "jack-seek", (mlt_transmitter) jack_seek_transmitter );
451 mlt_events_listen( properties, properties, "jack-start", (mlt_listener) on_jack_start );
452 mlt_events_listen( properties, properties, "jack-stop", (mlt_listener) on_jack_stop );
453 mlt_events_listen( properties, this, "jack-seek", (mlt_listener) on_jack_seek );
454 mlt_properties_set_position( properties, "_jack_seek", -1 );
458 mlt_log_error( NULL, "Failed to connect to JACK server\n" );
459 mlt_filter_close( this );