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 // Restore the original get_audio
240 frame
->get_audio
= mlt_frame_pop_audio( frame
);
242 int jack_frequency
= mlt_properties_get_int( filter_properties
, "_sample_rate" );
244 // Get the producer's audio
245 mlt_frame_get_audio( frame
, buffer
, format
, &jack_frequency
, channels
, samples
);
247 // TODO: Deal with sample rate differences
248 if ( *frequency
!= jack_frequency
)
249 fprintf( stderr
, "mismatching frequencies in filter jackrack\n" );
250 *frequency
= jack_frequency
;
252 // Initialise Jack ports and connections if needed
253 if ( mlt_properties_get_int( filter_properties
, "_samples" ) == 0 )
254 mlt_properties_set_int( filter_properties
, "_samples", *samples
);
256 // Get the filter-specific properties
257 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
258 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
259 pthread_mutex_t
*output_lock
= mlt_properties_get_data( filter_properties
, "output_lock", NULL
);
260 pthread_cond_t
*output_ready
= mlt_properties_get_data( filter_properties
, "output_ready", NULL
);
263 int16_t *q
= *buffer
;
267 // Convert to floats and write into output ringbuffer
268 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
270 for ( i
= 0; i
< *samples
; i
++ )
271 for ( j
= 0; j
< *channels
; j
++ )
273 sample
= ( float )( *q
++ ) / 32768.0;
274 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
278 // Synchronization phase - wait for signal from Jack process
279 while ( mlt_properties_get_int( filter_properties
, "_sync" )
280 && jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) )
281 pthread_cond_wait( output_ready
, output_lock
);
283 // Read from input ringbuffer and convert from floats
284 //if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
286 // Initialise to silence, but repeat last frame if available in case of
290 for ( i
= 0; i
< *samples
; i
++ )
291 for ( j
= 0; j
< *channels
; j
++ )
293 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
297 else if ( sample
< -1.0 )
301 *q
++ = 32767 * sample
;
303 *q
++ = 32768 * sample
;
311 /** Filter processing.
314 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
316 if ( frame
->get_audio
!= NULL
)
318 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
319 mlt_frame_push_audio( frame
, frame
->get_audio
);
320 mlt_frame_push_audio( frame
, this );
321 frame
->get_audio
= jackrack_get_audio
;
323 if ( mlt_properties_get_int( properties
, "_sync" ) )
324 initialise_jack_ports( properties
);
331 void filter_close( mlt_filter
this )
335 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
336 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
338 jack_deactivate( jack_client
);
339 jack_client_close( jack_client
);
340 for ( i
= 0; i
< mlt_properties_get_int( properties
, "channels" ); i
++ )
342 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
343 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
344 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
345 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
347 mlt_pool_release( mlt_properties_get_data( properties
, "output_buffers", NULL
) );
348 mlt_pool_release( mlt_properties_get_data( properties
, "input_buffers", NULL
) );
349 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_ports", NULL
) );
350 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_ports", NULL
) );
351 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_buffers", NULL
) );
352 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_buffers", NULL
) );
353 mlt_pool_release( mlt_properties_get_data( properties
, "output_lock", NULL
) );
354 mlt_pool_release( mlt_properties_get_data( properties
, "output_ready", NULL
) );
356 pthread_t
*jackrack_pthread
= mlt_properties_get_data( properties
, "jackrack_thread", NULL
);
357 if ( jackrack_pthread
!= NULL
)
359 mlt_properties_set_int( properties
, "_done", 1 );
360 pthread_join( *jackrack_pthread
, NULL
);
361 mlt_pool_release( jackrack_pthread
);
364 this->parent
.close
= NULL
;
365 mlt_service_close( &this->parent
);
368 /** Constructor for the filter.
371 mlt_filter
filter_jackrack_init( char *arg
)
373 mlt_filter
this = mlt_filter_new( );
378 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
379 jack_client_t
*jack_client
= jack_client_new( name
);
382 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
383 pthread_mutex_t
*output_lock
= mlt_pool_alloc( sizeof( pthread_mutex_t
) );
384 pthread_cond_t
*output_ready
= mlt_pool_alloc( sizeof( pthread_cond_t
) );
386 jack_set_process_callback( jack_client
, jack_process
, this );
387 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
388 this->process
= filter_process
;
389 this->close
= filter_close
;
390 pthread_mutex_init( output_lock
, NULL
);
391 pthread_cond_init( output_ready
, NULL
);
393 mlt_properties_set( properties
, "src", arg
);
394 mlt_properties_set( properties
, "_client_name", name
);
395 mlt_properties_set_data( properties
, "jack_client", jack_client
, 0, NULL
, NULL
);
396 mlt_properties_set_int( properties
, "_sample_rate", jack_get_sample_rate( jack_client
) );
397 mlt_properties_set_data( properties
, "output_lock", output_lock
, 0, NULL
, NULL
);
398 mlt_properties_set_data( properties
, "output_ready", output_ready
, 0, NULL
, NULL
);
399 mlt_properties_set_int( properties
, "_sync", 1 );
400 mlt_properties_set_int( properties
, "channels", 2 );