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>
35 #include "jack_rack.h"
37 #define BUFFER_LEN 204800 * 3
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, NULL, NULL );
58 mlt_properties_set( properties, "_rack_client_name", rack_name );
61 // Allocate buffers and ports
62 jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
63 jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
64 jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
65 jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
66 float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
67 float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
69 // Set properties - released inside filter_close
70 mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
71 mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
72 mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
73 mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
74 mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, NULL, NULL );
75 mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, NULL, NULL );
77 // Start Jack processing - required before registering ports
78 jack_activate( jack_client );
80 // Register Jack ports
81 for ( i = 0; i < channels; i++ )
85 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
86 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
87 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
88 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
89 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
90 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
92 for ( in = 0; in < 2; in++ )
94 snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
95 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
97 *port = jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
98 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
102 // Establish connections
103 for ( i = 0; i < channels; i++ )
106 for ( in = 0; in < 2; in++ )
108 port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
109 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
111 snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
112 if ( mlt_properties_get( properties, "_rack_client_name" ) )
113 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
114 else if ( mlt_properties_get( properties, rack_name ) )
115 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
117 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
121 fprintf( stderr, "jack connect %s to %s\n", rack_name, mlt_name );
122 jack_connect( jack_client, rack_name, mlt_name );
126 fprintf( stderr, "jack connect %s to %s\n", mlt_name, rack_name );
127 jack_connect( jack_client, mlt_name, rack_name );
133 static int jack_process (jack_nframes_t frames, void * data)
135 mlt_filter filter = (mlt_filter) data;
136 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
137 int channels = mlt_properties_get_int( properties, "channels" );
138 int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
139 int sync = mlt_properties_get_int( properties, "_sync" );
142 static int total_size = 0;
144 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
145 if ( output_buffers == NULL )
147 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
148 jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
149 jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
150 float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
151 float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
152 pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
153 pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
155 for ( i = 0; i < channels; i++ )
157 size_t jack_size = ( frames * sizeof(float) );
160 // Send audio through out port
161 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
162 if ( ! jack_output_buffers[i] )
164 fprintf( stderr, "%s: no jack buffer for output port %d\n", __FUNCTION__, i );
168 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
169 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
171 // Return audio through in port
172 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
173 if ( ! jack_input_buffers[i] )
175 fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
180 // Do not start returning audio until we have sent first mlt frame
181 if ( sync && i == 0 && frame_size > 0 )
182 total_size += ring_size;
183 //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
185 if ( ! sync || ( frame_size > 0 && total_size >= frame_size ) )
187 ring_size = jack_ringbuffer_write_space( input_buffers[i] );
188 jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
192 // Tell mlt that audio is available
193 pthread_mutex_lock( output_lock);
194 pthread_cond_signal( output_ready );
195 pthread_mutex_unlock( output_lock);
198 mlt_properties_set_int( properties, "_sync", 0 );
210 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
212 // Get the filter service
213 mlt_filter filter = mlt_frame_pop_audio( frame );
215 // Get the filter properties
216 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
218 int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
220 // Get the producer's audio
221 mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
223 // TODO: Deal with sample rate differences
224 if ( *frequency != jack_frequency )
225 fprintf( stderr, "mismatching frequencies in filter jackrack\n" );
226 *frequency = jack_frequency;
228 // Initialise Jack ports and connections if needed
229 if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
230 mlt_properties_set_int( filter_properties, "_samples", *samples );
232 // Get the filter-specific properties
233 jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
234 jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
235 // pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
236 // pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
239 int16_t *q = *buffer;
240 float sample[ 2 ][ 10000 ];
242 // struct timespec tm = { 0, 0 };
244 // Convert to floats and write into output ringbuffer
245 if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
247 for ( i = 0; i < *samples; i++ )
248 for ( j = 0; j < *channels; j++ )
249 sample[ j ][ i ] = ( float )( *q ++ ) / 32768.0;
251 for ( j = 0; j < *channels; j++ )
252 jack_ringbuffer_write( output_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
255 // Synchronization phase - wait for signal from Jack process
256 while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) ) ;
257 //pthread_cond_wait( output_ready, output_lock );
259 // Read from input ringbuffer and convert from floats
260 if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
262 // Initialise to silence, but repeat last frame if available in case of
264 for ( j = 0; j < *channels; j++ )
265 jack_ringbuffer_read( input_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
268 for ( i = 0; i < *samples; i++ )
269 for ( j = 0; j < *channels; j++ )
271 if ( sample[ j ][ i ] > 1.0 )
272 sample[ j ][ i ] = 1.0;
273 else if ( sample[ j ][ i ] < -1.0 )
274 sample[ j ][ i ] = -1.0;
276 if ( sample[ j ][ i ] > 0 )
277 *q ++ = 32767 * sample[ j ][ i ];
279 *q ++ = 32768 * sample[ j ][ i ];
287 /** Filter processing.
290 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
293 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
294 mlt_frame_push_audio( frame, this );
295 mlt_frame_push_audio( frame, jackrack_get_audio );
297 if ( mlt_properties_get_int( properties, "_sync" ) )
298 initialise_jack_ports( properties );
305 static void filter_close( mlt_filter this )
309 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
310 jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
312 jack_deactivate( jack_client );
313 jack_client_close( jack_client );
314 for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
316 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
317 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
318 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
319 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
321 mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
322 mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
323 mlt_pool_release( mlt_properties_get_data( properties, "jack_output_ports", NULL ) );
324 mlt_pool_release( mlt_properties_get_data( properties, "jack_input_ports", NULL ) );
325 mlt_pool_release( mlt_properties_get_data( properties, "jack_output_buffers", NULL ) );
326 mlt_pool_release( mlt_properties_get_data( properties, "jack_input_buffers", NULL ) );
327 mlt_pool_release( mlt_properties_get_data( properties, "output_lock", NULL ) );
328 mlt_pool_release( mlt_properties_get_data( properties, "output_ready", NULL ) );
330 jack_rack_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
331 jack_rack_destroy( jackrack );
333 this->parent.close = NULL;
334 mlt_service_close( &this->parent );
337 /** Constructor for the filter.
340 mlt_filter filter_jackrack_init( char *arg )
342 mlt_filter this = mlt_filter_new( );
347 snprintf( name, sizeof( name ), "mlt%d", getpid() );
348 jack_client_t *jack_client = jack_client_new( name );
351 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
352 pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
353 pthread_cond_t *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
355 jack_set_process_callback( jack_client, jack_process, this );
356 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
357 this->process = filter_process;
358 this->close = filter_close;
359 pthread_mutex_init( output_lock, NULL );
360 pthread_cond_init( output_ready, NULL );
362 mlt_properties_set( properties, "src", arg );
363 mlt_properties_set( properties, "_client_name", name );
364 mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
365 mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
366 mlt_properties_set_data( properties, "output_lock", output_lock, 0, NULL, NULL );
367 mlt_properties_set_data( properties, "output_ready", output_ready, 0, NULL, NULL );
368 mlt_properties_set_int( properties, "_sync", 1 );
369 mlt_properties_set_int( properties, "channels", 2 );