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_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
27 #include <sys/types.h>
30 #include <jack/jack.h>
31 #include <jack/ringbuffer.h>
35 #include "jack_rack.h"
37 #define BUFFER_LEN 204800 * 6
39 static void initialise_jack_ports( mlt_properties properties )
42 char mlt_name[20], rack_name[30];
43 jack_port_t **port = NULL;
44 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
45 jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
47 // Propogate these for the Jack processing callback
48 int channels = mlt_properties_get_int( properties, "channels" );
51 if ( mlt_properties_get( properties, "src" ) )
53 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
54 jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
55 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
57 mlt_properties_set_data( properties, "jackrack", jackrack, 0,
58 (mlt_destructor) jack_rack_destroy, NULL );
59 mlt_properties_set( properties, "_rack_client_name", rack_name );
62 // Allocate buffers and ports
63 jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
64 jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
65 jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
66 jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
67 float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
68 float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
70 // Set properties - released inside filter_close
71 mlt_properties_set_data( properties, "output_buffers", output_buffers,
72 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
73 mlt_properties_set_data( properties, "input_buffers", input_buffers,
74 sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
75 mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports,
76 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
77 mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports,
78 sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
79 mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers,
80 sizeof( float *) * channels, mlt_pool_release, NULL );
81 mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers,
82 sizeof( float *) * channels, mlt_pool_release, NULL );
84 // Start Jack processing - required before registering ports
85 jack_activate( jack_client );
87 // Register Jack ports
88 for ( i = 0; i < channels; i++ )
92 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
93 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
94 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
95 mlt_properties_set_data( properties, mlt_name, output_buffers[i],
96 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
97 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
98 mlt_properties_set_data( properties, mlt_name, input_buffers[i],
99 BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
101 for ( in = 0; in < 2; in++ )
103 snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
104 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
106 *port = jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
107 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
111 // Establish connections
112 for ( i = 0; i < channels; i++ )
115 for ( in = 0; in < 2; in++ )
117 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
118 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
120 snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
121 if ( mlt_properties_get( properties, "_rack_client_name" ) )
122 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
123 else if ( mlt_properties_get( properties, rack_name ) )
124 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
126 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
130 mlt_log_verbose( NULL, "JACK connect %s to %s\n", rack_name, mlt_name );
131 jack_connect( jack_client, rack_name, mlt_name );
135 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, rack_name );
136 jack_connect( jack_client, mlt_name, rack_name );
142 static int jack_process (jack_nframes_t frames, void * data)
144 mlt_filter filter = (mlt_filter) data;
145 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
146 int channels = mlt_properties_get_int( properties, "channels" );
147 int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
148 int sync = mlt_properties_get_int( properties, "_sync" );
151 static int total_size = 0;
153 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
154 if ( output_buffers == NULL )
156 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
157 jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
158 jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
159 float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
160 float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
161 pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
162 pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
164 for ( i = 0; i < channels; i++ )
166 size_t jack_size = ( frames * sizeof(float) );
169 // Send audio through out port
170 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
171 if ( ! jack_output_buffers[i] )
173 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for output port %d\n", i );
177 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
178 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
180 // Return audio through in port
181 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
182 if ( ! jack_input_buffers[i] )
184 mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for input port %d\n", i );
189 // Do not start returning audio until we have sent first mlt frame
190 if ( sync && i == 0 && frame_size > 0 )
191 total_size += ring_size;
192 mlt_log_debug( MLT_FILTER_SERVICE(filter), "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
194 if ( ! sync || ( frame_size > 0 && total_size >= frame_size ) )
196 ring_size = jack_ringbuffer_write_space( input_buffers[i] );
197 jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
201 // Tell mlt that audio is available
202 pthread_mutex_lock( output_lock);
203 pthread_cond_signal( output_ready );
204 pthread_mutex_unlock( output_lock);
207 mlt_properties_set_int( properties, "_sync", 0 );
219 static int jackrack_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
221 // Get the filter service
222 mlt_filter filter = mlt_frame_pop_audio( frame );
224 // Get the filter properties
225 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
227 int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
229 // Get the producer's audio
230 *format = mlt_audio_float;
231 mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
233 // TODO: Deal with sample rate differences
234 if ( *frequency != jack_frequency )
235 mlt_log_error( MLT_FILTER_SERVICE( filter ), "mismatching frequencies JACK = %d actual = %d\n",
236 jack_frequency, *frequency );
237 *frequency = jack_frequency;
239 // Initialise Jack ports and connections if needed
240 if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
241 mlt_properties_set_int( filter_properties, "_samples", *samples );
243 // Get the filter-specific properties
244 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
245 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
246 // pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
247 // pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
250 float *q = (float*) *buffer;
251 size_t size = *samples * sizeof(float);
253 // struct timespec tm = { 0, 0 };
255 // Write into output ringbuffer
256 for ( j = 0; j < *channels; j++ )
258 if ( jack_ringbuffer_write_space( output_buffers[j] ) >= size )
259 jack_ringbuffer_write( output_buffers[j], (char*)( q + j * *samples ), size );
262 // Synchronization phase - wait for signal from Jack process
263 while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < size ) ;
264 //pthread_cond_wait( output_ready, output_lock );
266 // Read from input ringbuffer
267 for ( j = 0; j < *channels; j++, q++ )
269 if ( jack_ringbuffer_read_space( input_buffers[j] ) >= size )
270 jack_ringbuffer_read( input_buffers[j], (char*)( q + j * *samples ), size );
277 /** Filter processing.
280 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
283 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
284 mlt_frame_push_audio( frame, this );
285 mlt_frame_push_audio( frame, jackrack_get_audio );
287 if ( !mlt_properties_get_data( properties, "jackrack", NULL ) )
288 initialise_jack_ports( properties );
295 static void filter_close( mlt_filter this )
297 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
298 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
299 jack_deactivate( jack_client );
300 jack_client_close( jack_client );
301 this->parent.close = NULL;
302 mlt_service_close( &this->parent );
305 /** Constructor for the filter.
308 mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
310 mlt_filter this = mlt_filter_new( );
315 snprintf( name, sizeof( name ), "mlt%d", getpid() );
316 jack_client_t *jack_client = jack_client_new( name );
319 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
320 pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
321 pthread_cond_t *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
323 jack_set_process_callback( jack_client, jack_process, this );
324 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
325 this->process = filter_process;
326 this->close = filter_close;
327 pthread_mutex_init( output_lock, NULL );
328 pthread_cond_init( output_ready, NULL );
330 mlt_properties_set( properties, "src", arg );
331 mlt_properties_set( properties, "_client_name", name );
332 mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
333 mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
334 mlt_properties_set_data( properties, "output_lock", output_lock, 0, mlt_pool_release, NULL );
335 mlt_properties_set_data( properties, "output_ready", output_ready, 0, mlt_pool_release, NULL );
336 mlt_properties_set_int( properties, "_sync", 1 );
337 mlt_properties_set_int( properties, "channels", 2 );